Tell me more ×
WordPress Answers is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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.

share|improve this question
1  
Are you sure that your $_SESSION['keywords'] is not empty and that this variable is an Array? – fischi Mar 18 at 11:41
thanks for the response, yes the session is populated, just tried 'terms' => "Australia", for each and it produces the same result, – ademc Mar 18 at 12:11
Have you tried 'terms' => array( 'australia' ), ? The slug is always in lowercase. – fischi Mar 18 at 12:43
Hi and welcome to WPSE. Could you please use the code MarkUp for every piece of code (instead of bold formatting or quoting)? Thanks. Please also do a var_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

Know someone who can answer? Share a link to this question via email, Google+, Twitter, or Facebook.

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.