Count posts that have a related term

Support MB Relationships Count posts that have a related termResolved

Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts
  • #31417
    Rebecca RobertsonRebecca Robertson
    Participant

    I have a CPT called Destinations.
    I have another CPT called Destination Posts.
    I have a custom taxonomy called States, this taxonomy is for the CPT Destination Posts.

    I have a relationship called Destination to State Taxonomy (id is from_destination_to_state_tax)
    that is a reciprocal relationship from Destination posts to The State taxonomy.

    On each post on the Destinations archive page I want to show the number of posts that have the taxonomy that is related to that destination.

    So, I have a Destination called "Exploring Arizona" and three Destination Posts with the State Taxonomy of "Arizona". On the Destinations archive page I want the "Exploring Arizona" post to show the count for all of the published Destination Post type posts that have the taxonomy term "Arizona".

    I've tried a couple of different ways of getting the post count but I am doing something wrong because neither of them are working right.

    This one shows all of the published Destination Posts, without regard to the state taxonomy:

    
    <?php
    $query = new WP_Query(
    $states = get_terms( [
    'taxonomy'     => 'state',
    'hide_empty'   => false,
    'status'     => 'publish',
    'relationship' => [
    'id'     => 'from_destination_to_state_tax',
    'from'       => get_the_ID('state'), // You can pass object ID or full object
    ],  
    ] ));
                        
    foreach ( $states as $state ) { 
    $count = $query->post_count;
    echo $count  . " Related Posts";
    }
    ?>
    

    This one shows the count for how many state taxonomy terms the Destination is related to, which is pretty much useless information as each Destination is only related to 1 state taxonomy term:

    
    <?php
    $terms  = get_terms( [
    'taxonomy'     => 'state',
    'hide_empty'   => false,
    'relationship' => [
    'id'         => 'from_destination_to_state_tax',
    'from'       => get_the_ID(), // You can pass object ID or full object
    ],
    ] );
                        
    foreach ( $terms as $term ) {
     wp_count_terms('state', $term) . "&nbsp; Related Posts";
    }
    ?>
    
    #31425
    Long NguyenLong Nguyen
    Moderator

    Hi Rebecca,

    You need to put the function get_terms() in the loop that queries Destinations posts. For example

     $query_destinations = new WP_query( array(
            'post_type' => 'destinations',
        ) );
    
        while( $query_destinations->have_posts() ) {
            $query_destinations->the_post(); 
            
            echo get_the_title();
            
            $terms  = get_terms( [
                'taxonomy'     => 'state',
                'hide_empty'   => false,
                'relationship' => [
                    'id'         => 'from_destination_to_state_tax',
                    'from'       => get_the_ID(), // You can change from or to based on the relationship
                ],
            ] );
    
            foreach ( $terms as $term ) {
                echo $term->slug . ' - ' . $term->count . "&nbsp; Related Posts";
            }
        }
    #31432
    Rebecca RobertsonRebecca Robertson
    Participant

    Thank you, that helped!

    I had to make a few modifications as your code showed post counts for all of the taxonomy terms and I only wanted the count for the single related term.

    This is the code that works:

    <?php
    $query_destinations = new WP_query( array(
    'post_type' => 'destination',
    ) );
            
    $states = get_terms( [
    'taxonomy'     => 'state',
    'hide_empty'   => false,
    'relationship' => [
    'id'           => 'from_destination_to_state_tax',
    'from'         => get_the_ID(), // You can change from or to based on the relationship
    ],
    ] );
    
    foreach ( $states as $state ) {
    $count = $query->post_count;
    echo  $state->count . " Related Posts";
    }
    ?>         
    
Viewing 3 posts - 1 through 3 (of 3 total)
  • You must be logged in to reply to this topic.