• Resolved formica

    (@formica)


    Please can someone help I’m totally confused. I need to list posts with multiple taxonomies ie.

    List custom posts of ‘Therapists’, by taxonomies of ‘location’, and by ‘therapy’.

    arrghh… Please help.

    $args=array(
     'post_type' => 'therapist',
     'therapies' => 'acupressure',
     'location' => 'bristol',
     'post_status' => 'publish',
     );
    
    $my_query = null;
    $my_query = new WP_Query($args);
    if( $my_query->have_posts() ) {
    while ($my_query->have_posts()) : $my_query->the_post();

    do something…

Viewing 15 replies - 1 through 15 (of 16 total)
  • Moderator keesiemeijer

    (@keesiemeijer)

    try it with a tax_query: https://codex.www.ads-software.com/Function_Reference/WP_Query#Taxonomy_Parameters

    something like this:

    $args = array(
      'post_type' => 'therapist',
      'post_status' => 'publish',
      'tax_query' => array(
    
    		array(
    			'taxonomy' => 'therapies',
    			'field' => 'slug',
    			'terms' => array( 'acupressure' )
    		),
    		array(
    			'taxonomy' => 'location',
    			'field' => 'slug',
    			'terms' => array( 'bristol' )
    		)
    	)
    );
    
    $my_query = new WP_Query( $args );

    Thread Starter formica

    (@formica)

    And relax…. thank you keesiemeijer. Seriously appreciated.

    Got myself in a serious muddle with taxonomies, I still am but I can see a bit of light now.

    Sorry last question, is there a way for it to dynamically get the current taxonomy of ‘therapies’ itself. I’m showing it on a post that’s already been tagged with the taxonomy of ‘acupressure’.

    ie. can get the current taxonomy for ‘therapy’ itself?

    Thanks.

    Moderator keesiemeijer

    (@keesiemeijer)

    You can get the current category with this:

    $current_cat = get_query_var('cat'); // category ID
    $current_cat_slug = get_query_var('category_name'); // category slug (only on category page)

    or maybe this with custom taxonomies:

    // get queried object
    $object = get_queried_object();
    if($object->taxonomy == 'therapy' ) {
    $current_tax_term_slug = $object->slug; // term slug
    $current_tax_term_id = $object->term_id; // term ID
    }

    Thread Starter formica

    (@formica)

    Hmmm… Thanks for your help but I’m Sorry I’m still struggling.

    How would I get it in the array, such as:

    array(
     'taxonomy' => 'therapies',
     'field' => 'slug',
     'terms' => $object->slug
    ),

    Perhaps more worryingly if I echo the ‘$current_tax_term_id’ elsewhere on the post i get nothing.

    I’ve registered the taxonomy ‘therapies’ under my custom post type of ‘therapists’. All seems to work, however I’ve also allowed the taxonomy ‘therapies’ to be under the standard wordpress ‘post’ type.

    So I’ve then created a standard post called ‘Acupressure’ and tagged it as ‘acupressure’ in the taxonomy of ‘therapies’. So why does it not echo it’s taxonomy term?

    I’m so confused ??

    Moderator keesiemeijer

    (@keesiemeijer)

    I’m showing it on a post that’s already been tagged with the taxonomy of ‘acupressure’.

    On what template file are you using this? Are you using this on a archive template or single.php?

    Thread Starter formica

    (@formica)

    Hi, it’s on single.php

    Moderator keesiemeijer

    (@keesiemeijer)

    My mistake. On single.php $object = get_queried_object(); is not a taxonomy object but a post object. You can check if the post on single.php has any terms of the “therapies” taxonomy with this:

    $terms = wp_get_post_terms( $post->ID, 'therapies' );
    if($terms){
    // post has therapies terms attached
    }

    https://codex.www.ads-software.com/Function_Reference/wp_get_post_terms

    Thread Starter formica

    (@formica)

    Ahhh… makes sense. Thankyou. Sorry for not fully explaining, the current template didn’t occur to me. I’m now so close.

    If i print_r that i get:

    Array ( [0] => stdClass Object ( [term_id] => 6 [name] => Acupressure [slug] => acupressure [term_group] => 0 [term_taxonomy_id] => 12 [taxonomy] => therapies [description] => [parent] => 0 [count] => 2 ) )

    Which i encouraging as it’s picking up the term.

    All i need to do now is get that term in your above query

    $args = array(
      'post_type' => 'therapist',
      'post_status' => 'publish',
      'tax_query' => array(
    		array(
    			'taxonomy' => 'therapies',
    			'field' => 'slug',
    			'terms' => // get the current term name
    		),
    		array(
    			'taxonomy' => 'location',
    			'field' => 'slug',
    			'terms' => array( 'bristol' )
    		)
    	)
    );
    
    $my_query = new WP_Query( $args );

    Thanks for helping – i can finally see the end.

    Moderator keesiemeijer

    (@keesiemeijer)

    try it with this: https://pastebin.com/riUCiCm0

    Thread Starter formica

    (@formica)

    Thanks. I can see that’s exactly what i need but unfortunately it throws up an error:

    Parse error: syntax error, unexpected ‘=’, expecting ‘&’ or T_STRING or T_VARIABLE or ‘$’ in /../single.php on line 51

    Moderator keesiemeijer

    (@keesiemeijer)

    Oops, change this:

    foreach ($therapy_terms as => $term){

    to this:

    foreach ($therapy_terms as $term){

    Thread Starter formica

    (@formica)

    Aw.. so close, I can see why it would work but it’s now displaying nothing. No errors all’s fine but just not outputting the name.

    https://pastebin.com/uX3tXeNq

    Moderator keesiemeijer

    (@keesiemeijer)

    I need more coffee. Another mistake.
    change this:

    foreach ($therapy_terms as $term){

    to this:

    foreach ($terms as $term){

    Thread Starter formica

    (@formica)

    Holy Mother of God!

    Even minus some caffeine you’re an unfettered genius!

    I can’t thank you enough.

    Thankyou thankyou and thankyou.

    Moderator keesiemeijer

    (@keesiemeijer)

    You’re welcome. Glad you got it resolved.

Viewing 15 replies - 1 through 15 (of 16 total)
  • The topic ‘Multiple taxonomies’ is closed to new replies.