I have 2 custom post types in Wordpress: Destinations and Tours (so say Paris and Day Trip to Paris). They have a many to many relationship. Now I need to count the number of tours associated with the individual destinations like, Paris(2), Rome(5) etc.

I tried to use this


but this gets the total number of the tours for all the destinations. So the question is, how can I get only the number of tours associated with individual destinations?


  • how have you created these many to many connections?
    – Milo
    Jun 8 '17 at 15:38

Sounds like you need to use WP_Query or something similar.

// First query: get all destinations
$args = array(
    'post_type' => 'destination',
    'posts_per_page' => -1,
    'no_found_rows' => 'true' // skips pagination
$destinations = new WP_Query($args);
// Now for every destination:
foreach($destinations as $destination) {
    // Second query to get tours
    $secondargs = array(
        'post_type' => 'tour',
        'posts_per_page' => -1,
        'no_found_rows' => 'true', // skip pagination
        'meta_query' => array(
            array('key' => 'yourmetakey',
                'value' => "$destination", // only get tours with current destination
                'compare' => '=',
    $tours = new WP_Query($secondargs);
    // output the name of the destination plus the number of tours
    echo $destination->title . '(' . count($tours) . ')';

The first query grabs all destinations. The second query will need to be tweaked - however you've associated the destinations to the tours, use that postmeta or taxonomy to only grab the tours for each individual destination. At the end, you'll probably want to customize the html output - maybe a <ul> or something similar, probably linking to the destination.

  • First of all, thanks for your answer @WebElaine. I implemented your suggested solution but it's always returning 1 tour for every destination (although there are destinations with more than 1 tour). I'm probably doing something wrong here although I used the postmeta as you suggested ...
    – viblic
    Jun 8 '17 at 16:14
  • As Milo asked - how does WordPress know that 'tour' and 'destination' are associated? Are you using ACF or postmeta to create that relationship, or perhaps a taxonomy? As noted, this code will not work unless you update it with the actual connection so that WP knows which tours to pull per destination. WP does not know "Paris Day Trip" belongs to "Paris" unless you explicitly connect them somehow.
    – WebElaine
    Jun 8 '17 at 17:48
  • Well, truth be told I don't really know that because I'm using a theme and it has this sort of functionality built-in and I don't know much about wordpress as well. However, I suspect it is postmeta because I did this and it is working correctly: code $tours = get_post_meta( $destination_ID, $key = 'destination_tour' ); $tours_count = count($tours, COUNT_RECURSIVE) - 1; code Now it is working as expected but I don't really know if this is the right way to do it ...
    – viblic
    Jun 8 '17 at 22:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.