I've got a custom post type for training courses. For those courses I've got taxonomies of "qualification-level", "training-country" and "worker-cardre".
I'm trying to retrieve posts based on a keyword that matches in any of the taxonomies
eg. for the keyword of "Australia" it should return posts that match "Australia" for "qualification-level" OR "training-country" OR "worker-cardre". The code I'm using for the tax_query is below:
$args = array();
$args['post_type'] = "training";
$args['showposts'] = 15;
$args['paged'] = $paged;
$args['orderby'] = "title";
$args['order'] = "ASC";
if( ! empty( $_SESSION['keywords'] ) ) {
$args['tax_query'] = array(
'relation' => 'OR',
array(
'taxonomy' => 'qualification-level',
'terms' => array($_SESSION['keywords']),
'field' => 'slug',
),
array(
'taxonomy' => 'training-country',
'terms' => array($_SESSION['keywords']),
'field' => 'slug',
),
array(
'taxonomy' => 'worker-cadre',
'terms' => array($_SESSION['keywords']),
'field' => 'slug',
),
);
}
$search_query = new WP_Query($args);
It is currently returning all results, even those that don't match the keyword (eg, Vietnam).
I've also tried with the 'operator' => 'IN' for each but that doesn't seem to work either (still shows all results)
Can anyone help?
Thanks.
UPDATE 1: I tried:
$terms = wp_get_post_terms($post->ID, 'training-country');
var_dump($terms);
as suggested by @kaiser which outputs the following:
array(1) { [0]=> object(stdClass)#1501 (9) { ["term_id"]=> string(1) "4" ["name"]=> string(9) "Australia" ["slug"]=> string(9) "australia" ["term_group"]=> string(1) "0" ["term_taxonomy_id"]=> string(1) "4" ["taxonomy"]=> string(16) "training-country" ["description"]=> string(0) "" ["parent"]=> string(1) "0" ["count"]=> string(2) "79" } }
for one post retrieved and:
array(1) { [0]=> object(stdClass)#1504 (9) { ["term_id"]=> string(2) "34" ["name"]=> string(7) "Vietnam" ["slug"]=> string(7) "vietnam" ["term_group"]=> string(1) "0" ["term_taxonomy_id"]=> string(2) "34" ["taxonomy"]=> string(16) "training-country" ["description"]=> string(0) "" ["parent"]=> string(1) "0" ["count"]=> string(2) "66" } }
for another. Still no idea whats happening or why.
$_SESSION['keywords']is not empty and that this variable is anArray? – fischi Mar 18 at 11:41'terms' => array( 'australia' ),? The slug is always in lowercase. – fischi Mar 18 at 12:43var_dump()of the term object and check if your$_SESSION['keywords']array really holds the same strings as you find in$term->slug. – kaiser Mar 18 at 13:12