How to search for a string in a category name?
up vote
0
down vote
favorite
I use the search result and I GET
$s = $_GET['s'];
Which gives: "mario"
Now there is a category named Mario Bianchi
under a main category called Persone
When I GET $s
I need to get all posts within that category, I tried the following but I get nothing
$terms = get_terms( 'category', array(
'name__like' => $s,
'hide_empty' => true // Optional
) );
if ( count($terms) > 0 ){
echo '<ul>';
foreach ( $terms as $term ) {
echo '<li><a href="' . esc_url( get_term_link( $term ) ) . '" title="' . esc_attr( $term->name ) . '">' . esc_html( $term->name ) . '</a></li>';
}
echo '</ul>';
}
Yet I need the actual post attached, not the category itself
php wordpress
add a comment |
up vote
0
down vote
favorite
I use the search result and I GET
$s = $_GET['s'];
Which gives: "mario"
Now there is a category named Mario Bianchi
under a main category called Persone
When I GET $s
I need to get all posts within that category, I tried the following but I get nothing
$terms = get_terms( 'category', array(
'name__like' => $s,
'hide_empty' => true // Optional
) );
if ( count($terms) > 0 ){
echo '<ul>';
foreach ( $terms as $term ) {
echo '<li><a href="' . esc_url( get_term_link( $term ) ) . '" title="' . esc_attr( $term->name ) . '">' . esc_html( $term->name ) . '</a></li>';
}
echo '</ul>';
}
Yet I need the actual post attached, not the category itself
php wordpress
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I use the search result and I GET
$s = $_GET['s'];
Which gives: "mario"
Now there is a category named Mario Bianchi
under a main category called Persone
When I GET $s
I need to get all posts within that category, I tried the following but I get nothing
$terms = get_terms( 'category', array(
'name__like' => $s,
'hide_empty' => true // Optional
) );
if ( count($terms) > 0 ){
echo '<ul>';
foreach ( $terms as $term ) {
echo '<li><a href="' . esc_url( get_term_link( $term ) ) . '" title="' . esc_attr( $term->name ) . '">' . esc_html( $term->name ) . '</a></li>';
}
echo '</ul>';
}
Yet I need the actual post attached, not the category itself
php wordpress
I use the search result and I GET
$s = $_GET['s'];
Which gives: "mario"
Now there is a category named Mario Bianchi
under a main category called Persone
When I GET $s
I need to get all posts within that category, I tried the following but I get nothing
$terms = get_terms( 'category', array(
'name__like' => $s,
'hide_empty' => true // Optional
) );
if ( count($terms) > 0 ){
echo '<ul>';
foreach ( $terms as $term ) {
echo '<li><a href="' . esc_url( get_term_link( $term ) ) . '" title="' . esc_attr( $term->name ) . '">' . esc_html( $term->name ) . '</a></li>';
}
echo '</ul>';
}
Yet I need the actual post attached, not the category itself
php wordpress
php wordpress
edited Nov 15 at 1:53
asked Nov 15 at 1:48
rob.m
3,639103782
3,639103782
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
up vote
0
down vote
accepted
get_terms
does just that, it get the terms for your query. What you'll want is WP_Query
with a tax_query
. You've already got the categories you want to return the posts for, so it shouldn't be too difficult
$terms = get_terms( array(
'taxonomy' => 'category',
'name__like' => $s,
'hide_empty' => true // Optional
) );
$term_ids = array();
if ( ! empty( $terms ) ) {
foreach( $terms as $term ) {
$term_ids = $term->term_id;
}
$args = array(
'post_type' => 'post',
'tax_query' => array(
array(
'taxonomy' => 'category',
'terms' => $term_ids,
),
),
);
$query = new WP_Query( $args );
if ( $query->have_posts() ) {
echo '<ul>';
while ( $query->have_posts() ) {
$query->the_post();
echo '<li><a href="' . esc_url( get_the_permalink() ) . '" title="' . get_the_title() . '">' . esc_html( get_the_title() ) . '</a></li>';
}
echo '</ul>';
}
wp_reset_postdata();
}
ok let me try this, p.s.syntax error, unexpected '{'
onif ( ! empty( $terms ) {
– rob.m
Nov 15 at 2:05
it is not looking in category, only finds a match of$s
in a title, but let's say I search formario
and I have a category calledmario bianchi
, I need all posts within that category since we found a match of$s
(mario) in a category since we have onemario bianchi
– rob.m
Nov 15 at 2:07
if I do$term_ids = array(); var_dump($terms);
iget array(0) {}
but we do have a category calledmario bianchi
– rob.m
Nov 15 at 2:14
@rob.m It seems to be working for me on a test site: xanthous-lion.w5.poopy.life/?s=mario
– cameronjonesweb
Nov 15 at 2:23
in your link i see "NOTHING FOUND Hello world! Sorry, but nothing matched your search terms. Please try again with some different keywords."
– rob.m
Nov 15 at 2:24
|
show 6 more comments
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
accepted
get_terms
does just that, it get the terms for your query. What you'll want is WP_Query
with a tax_query
. You've already got the categories you want to return the posts for, so it shouldn't be too difficult
$terms = get_terms( array(
'taxonomy' => 'category',
'name__like' => $s,
'hide_empty' => true // Optional
) );
$term_ids = array();
if ( ! empty( $terms ) ) {
foreach( $terms as $term ) {
$term_ids = $term->term_id;
}
$args = array(
'post_type' => 'post',
'tax_query' => array(
array(
'taxonomy' => 'category',
'terms' => $term_ids,
),
),
);
$query = new WP_Query( $args );
if ( $query->have_posts() ) {
echo '<ul>';
while ( $query->have_posts() ) {
$query->the_post();
echo '<li><a href="' . esc_url( get_the_permalink() ) . '" title="' . get_the_title() . '">' . esc_html( get_the_title() ) . '</a></li>';
}
echo '</ul>';
}
wp_reset_postdata();
}
ok let me try this, p.s.syntax error, unexpected '{'
onif ( ! empty( $terms ) {
– rob.m
Nov 15 at 2:05
it is not looking in category, only finds a match of$s
in a title, but let's say I search formario
and I have a category calledmario bianchi
, I need all posts within that category since we found a match of$s
(mario) in a category since we have onemario bianchi
– rob.m
Nov 15 at 2:07
if I do$term_ids = array(); var_dump($terms);
iget array(0) {}
but we do have a category calledmario bianchi
– rob.m
Nov 15 at 2:14
@rob.m It seems to be working for me on a test site: xanthous-lion.w5.poopy.life/?s=mario
– cameronjonesweb
Nov 15 at 2:23
in your link i see "NOTHING FOUND Hello world! Sorry, but nothing matched your search terms. Please try again with some different keywords."
– rob.m
Nov 15 at 2:24
|
show 6 more comments
up vote
0
down vote
accepted
get_terms
does just that, it get the terms for your query. What you'll want is WP_Query
with a tax_query
. You've already got the categories you want to return the posts for, so it shouldn't be too difficult
$terms = get_terms( array(
'taxonomy' => 'category',
'name__like' => $s,
'hide_empty' => true // Optional
) );
$term_ids = array();
if ( ! empty( $terms ) ) {
foreach( $terms as $term ) {
$term_ids = $term->term_id;
}
$args = array(
'post_type' => 'post',
'tax_query' => array(
array(
'taxonomy' => 'category',
'terms' => $term_ids,
),
),
);
$query = new WP_Query( $args );
if ( $query->have_posts() ) {
echo '<ul>';
while ( $query->have_posts() ) {
$query->the_post();
echo '<li><a href="' . esc_url( get_the_permalink() ) . '" title="' . get_the_title() . '">' . esc_html( get_the_title() ) . '</a></li>';
}
echo '</ul>';
}
wp_reset_postdata();
}
ok let me try this, p.s.syntax error, unexpected '{'
onif ( ! empty( $terms ) {
– rob.m
Nov 15 at 2:05
it is not looking in category, only finds a match of$s
in a title, but let's say I search formario
and I have a category calledmario bianchi
, I need all posts within that category since we found a match of$s
(mario) in a category since we have onemario bianchi
– rob.m
Nov 15 at 2:07
if I do$term_ids = array(); var_dump($terms);
iget array(0) {}
but we do have a category calledmario bianchi
– rob.m
Nov 15 at 2:14
@rob.m It seems to be working for me on a test site: xanthous-lion.w5.poopy.life/?s=mario
– cameronjonesweb
Nov 15 at 2:23
in your link i see "NOTHING FOUND Hello world! Sorry, but nothing matched your search terms. Please try again with some different keywords."
– rob.m
Nov 15 at 2:24
|
show 6 more comments
up vote
0
down vote
accepted
up vote
0
down vote
accepted
get_terms
does just that, it get the terms for your query. What you'll want is WP_Query
with a tax_query
. You've already got the categories you want to return the posts for, so it shouldn't be too difficult
$terms = get_terms( array(
'taxonomy' => 'category',
'name__like' => $s,
'hide_empty' => true // Optional
) );
$term_ids = array();
if ( ! empty( $terms ) ) {
foreach( $terms as $term ) {
$term_ids = $term->term_id;
}
$args = array(
'post_type' => 'post',
'tax_query' => array(
array(
'taxonomy' => 'category',
'terms' => $term_ids,
),
),
);
$query = new WP_Query( $args );
if ( $query->have_posts() ) {
echo '<ul>';
while ( $query->have_posts() ) {
$query->the_post();
echo '<li><a href="' . esc_url( get_the_permalink() ) . '" title="' . get_the_title() . '">' . esc_html( get_the_title() ) . '</a></li>';
}
echo '</ul>';
}
wp_reset_postdata();
}
get_terms
does just that, it get the terms for your query. What you'll want is WP_Query
with a tax_query
. You've already got the categories you want to return the posts for, so it shouldn't be too difficult
$terms = get_terms( array(
'taxonomy' => 'category',
'name__like' => $s,
'hide_empty' => true // Optional
) );
$term_ids = array();
if ( ! empty( $terms ) ) {
foreach( $terms as $term ) {
$term_ids = $term->term_id;
}
$args = array(
'post_type' => 'post',
'tax_query' => array(
array(
'taxonomy' => 'category',
'terms' => $term_ids,
),
),
);
$query = new WP_Query( $args );
if ( $query->have_posts() ) {
echo '<ul>';
while ( $query->have_posts() ) {
$query->the_post();
echo '<li><a href="' . esc_url( get_the_permalink() ) . '" title="' . get_the_title() . '">' . esc_html( get_the_title() ) . '</a></li>';
}
echo '</ul>';
}
wp_reset_postdata();
}
edited Nov 15 at 2:18
answered Nov 15 at 2:03
cameronjonesweb
1,64921631
1,64921631
ok let me try this, p.s.syntax error, unexpected '{'
onif ( ! empty( $terms ) {
– rob.m
Nov 15 at 2:05
it is not looking in category, only finds a match of$s
in a title, but let's say I search formario
and I have a category calledmario bianchi
, I need all posts within that category since we found a match of$s
(mario) in a category since we have onemario bianchi
– rob.m
Nov 15 at 2:07
if I do$term_ids = array(); var_dump($terms);
iget array(0) {}
but we do have a category calledmario bianchi
– rob.m
Nov 15 at 2:14
@rob.m It seems to be working for me on a test site: xanthous-lion.w5.poopy.life/?s=mario
– cameronjonesweb
Nov 15 at 2:23
in your link i see "NOTHING FOUND Hello world! Sorry, but nothing matched your search terms. Please try again with some different keywords."
– rob.m
Nov 15 at 2:24
|
show 6 more comments
ok let me try this, p.s.syntax error, unexpected '{'
onif ( ! empty( $terms ) {
– rob.m
Nov 15 at 2:05
it is not looking in category, only finds a match of$s
in a title, but let's say I search formario
and I have a category calledmario bianchi
, I need all posts within that category since we found a match of$s
(mario) in a category since we have onemario bianchi
– rob.m
Nov 15 at 2:07
if I do$term_ids = array(); var_dump($terms);
iget array(0) {}
but we do have a category calledmario bianchi
– rob.m
Nov 15 at 2:14
@rob.m It seems to be working for me on a test site: xanthous-lion.w5.poopy.life/?s=mario
– cameronjonesweb
Nov 15 at 2:23
in your link i see "NOTHING FOUND Hello world! Sorry, but nothing matched your search terms. Please try again with some different keywords."
– rob.m
Nov 15 at 2:24
ok let me try this, p.s.
syntax error, unexpected '{'
on if ( ! empty( $terms ) {
– rob.m
Nov 15 at 2:05
ok let me try this, p.s.
syntax error, unexpected '{'
on if ( ! empty( $terms ) {
– rob.m
Nov 15 at 2:05
it is not looking in category, only finds a match of
$s
in a title, but let's say I search for mario
and I have a category called mario bianchi
, I need all posts within that category since we found a match of $s
(mario) in a category since we have one mario bianchi
– rob.m
Nov 15 at 2:07
it is not looking in category, only finds a match of
$s
in a title, but let's say I search for mario
and I have a category called mario bianchi
, I need all posts within that category since we found a match of $s
(mario) in a category since we have one mario bianchi
– rob.m
Nov 15 at 2:07
if I do
$term_ids = array(); var_dump($terms);
i get array(0) {}
but we do have a category called mario bianchi
– rob.m
Nov 15 at 2:14
if I do
$term_ids = array(); var_dump($terms);
i get array(0) {}
but we do have a category called mario bianchi
– rob.m
Nov 15 at 2:14
@rob.m It seems to be working for me on a test site: xanthous-lion.w5.poopy.life/?s=mario
– cameronjonesweb
Nov 15 at 2:23
@rob.m It seems to be working for me on a test site: xanthous-lion.w5.poopy.life/?s=mario
– cameronjonesweb
Nov 15 at 2:23
in your link i see "NOTHING FOUND Hello world! Sorry, but nothing matched your search terms. Please try again with some different keywords."
– rob.m
Nov 15 at 2:24
in your link i see "NOTHING FOUND Hello world! Sorry, but nothing matched your search terms. Please try again with some different keywords."
– rob.m
Nov 15 at 2:24
|
show 6 more comments
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53311303%2fhow-to-search-for-a-string-in-a-category-name%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown