Is there any 'native' way to query by keyword or post tag?
If not, how can I write a GOOD SQL query to retrieve the data correctly?
Thanks in advance!
Is there any 'native' way to query by keyword or post tag?
If not, how can I write a GOOD SQL query to retrieve the data correctly?
Thanks in advance!
Depending what you want to achieve, the most simple way taken from Wordpress documentation looks like below.
Displays posts tagged with "bob", under "people" custom taxonomy:
$args = array(
'post_type' => 'post',
'tax_query' => array(
array(
'taxonomy' => 'people',
'field' => 'slug',
'terms' => 'bob',
),
),
);
$query = new WP_Query( $args );
Check the documentation link - you'll find more advanced examples.
Update:
If you'd like to query actually for both 'keyword' or 'tag'...
It's funny - I did some research and it turns out that it isn't so trivial in Wordpress.
The most clean looking, native solution I found uses three queries. The first two get the ids - one by tag and the other by keyword, and then the third make the final query.
$set1 = new WP_Query( array('
'fields'=> 'ids',
'post_type' => 'post',
'tax_query' => array(
array(
'taxonomy' => 'people',
'field' => 'slug',
'terms' => 'bob'
)
)
)
);
$set2 = new WP_Query( array(
'fields'=>'ids',
'post_type' => 'post',
's' => 'News')
);
$combined_ids = array_merge($set1->posts, $set2->posts);
$combined_ids = array_unique($combined_ids);
$combines_sets = new WP_Query(array(
'post__in' => $combined_ids)
);