• Hello

    I have 2 CPT
    1. Organisation
    2. products.

    I also created a taxonomy called City that is used for both CPT.

    I have products that are sold only in specific cities. On the Locations single page, I would like to display 3 of the products sold in that specific location. So I need to query posts with Product CPT AND same taxonomy (city) as the current Organisation.

    For example, on the single page of the Organization in New York, I want to display a max of 3 products sold only in New York.

    Usually I would use WP_Query to display the posts from products with the term New York and add it to the Organisation Single page. However, I would like WordPress to check the current taxonomy of the single Organisation page and display max 3 of the product with the SAME taxonomy.

    Is it possible to do it? If yes, please could you help?

Viewing 6 replies - 1 through 6 (of 6 total)
  • Moderator bcworkz

    (@bcworkz)

    Yes you can! You just need to get the term assigned to the current organisation post with wp_get_post_terms(). This returns an array of terms, even if only one is assigned. The first term has the [0] index into the array. With the assigned term, make a WP_Query as you normally would for products assigned that same term.

    You do need the current organisation post’s ID to get its related terms. On singular pages or in the loop, get_the_ID() will return the desired post ID.

    Thread Starter morena_loca

    (@morena_loca)

    Hey there!
    Thanks very much for your reply. But I didn’t quite get it. 🙁

    I came up with this but it’s not working

    
    // In single-organisation.php To display last 3 products sold in the same city as the organisation
    
    <?php
                    $custom_terms = wp_get_post_terms('city');
                    $args = array(
                      'post_type' => 'product',
                      'tax_query' => array(             
                           array(
                              'taxonomy' => 'city',
                              'posts_per_page' => 3,
                              'field' => 'slug',
                              'terms' => $custom_terms[0]->slug, 
                          ),
                       )
                    );
    
                    $loop = new WP_Query( $args );
    
                    while ( $loop->have_posts() ) : $loop->the_post();
                        get_template_part( 'template-parts/content');
                    endwhile;
    
                    wp_reset_postdata();
                ?>
    

    Also, I have another question, These elements will be in a <section> how do I test if any post has been published in products/city ? If there are no products available, I would just add a message to say so.

    Thanks a lot

    Moderator bcworkz

    (@bcworkz)

    Good effort! One correction I think:
    $custom_terms = wp_get_post_terms( get_the_ID(), 'city');

    To capture when there are no posts returned by the query, wrap your while loop in the first part of an if/else conditional. Your theme likely has an example on its templates (index.php perhaps), you’d just check with $loop->have_posts() instead of have_posts().

    This checks for products with the same city term assigned that the current post has.

    Thread Starter morena_loca

    (@morena_loca)

    Hey!
    Thanks so much for your help. I tried that but it is not working. I have no idea why

    // In single-organisation.php To display last 3 products sold in the same city as the organisation
    
    <?php
                    $custom_terms = wp_get_post_terms( get_the_ID(), 'city');
                    $args = array(
                      'post_type' => 'product',
                      'tax_query' => array(             
                           array(
                              'taxonomy' => 'city',
                              'posts_per_page' => 3,
                              'field' => 'slug',
                              'terms' => $custom_terms[0]->slug, 
                          ),
                       )
                    );
    
                    $loop = new WP_Query( $args );
    
                    while ( $loop->have_posts() ) : $loop->the_post();
                        get_template_part( 'template-parts/content');
                    endwhile;
    
                    wp_reset_postdata();
                ?>

    Any idea what I am missing here?

    Thanks for the $loop answer, obviously I forgot about that he he he.

    • This reply was modified 3 years, 2 months ago by morena_loca.
    Moderator bcworkz

    (@bcworkz)

    Your code looks fine. There could be an issue with context. Confirm echo get_the_ID(); outputs the right post ID. I’ve no idea what the template part does. It’s probably fine, but add the_title(); within the loop to verify there are products returned by the query.

    Thread Starter morena_loca

    (@morena_loca)

    Hey there!

    Thanks so much. Ok I am going to check all that. It should be working. There must me a typo somewhere.

    Thanks a lot for your help!

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Query CPT that share the same taxonomy as the current CPT’ is closed to new replies.