0

Querying, storing, and using data from 2 separate custom post types

I have 2 CPT: Term and Location

In my query(ies), I need to get information from both CPT.

For CPT ‘Term’; I need to know if it’s ‘active’ and the post_title() if it is. For CPT; ’Location’ I need to know if it’s ‘active’ and the post_title() if it is.

Not only that - if there are more than 1 ‘Location’ I need to display all of them (if they’re active).

Any ideas on how to tackle this beast?

Below is the query (followed by the flow the looped html will display for reference) – but it totally doesn’t work:

    $centre_type = get_field('site_primary_centre_type', 'option');
    $args = array(
    'post_type' => array('gym_location', 'gym_term'),
    'orderby' => 'title',
    'order' => 'DESC',
      'meta_query' => array(
                  array(
                  'key' => array('term_active', 'location_active'),
                  'value' => '1',
                  'compare' => '=='
                  )
        )
    );

    $query = new WP_Query( $args );

    if($query->have_posts()) : while ( $query->have_posts() ) : $query->the_post();

    <h5>term_title Timetable</h5>
    if ((1 location) && (location && term == active)) :
      [wcs location="location_title" term="term_title" style="responsive"]
    else 

    if ((more than 1 location) && (location && term == active)) :
      [su_tabs][su_tab title="location_title <?php echo $centre_type; ?>"]
      [wcs location="location_title" term="term_title" style="responsive"]
      [/su_tab]
      [/su_tabs]

  <?php endwhile; endif; // end of query ?>

1 Answer 1

0

For custom field queries you can't use an array for multiple keys, unfortunately. What you want is something like this:

$args = array(
    'post_type' => array('gym_location', 'gym_term'),
    'orderby' => 'title',
    'order' => 'DESC',
    'meta_query' => array(
        array(
            'key' => 'term_active',
            'value' => '1',
            'compare' => '=='
        ),
        array(
            'key' => 'location_active',
            'value' => '1',
            'compare' => '=='
        ),
        'relation' => 'OR',
    )
);

See https://codex.wordpress.org/Class_Reference/WP_Query#Custom_Field_Parameters

3
  • This works. But not in the way I need it to work. I need to pull 'the_title();' from each CPT and display it in the html. Commented Aug 25, 2016 at 1:45
  • Inside your custom loop you can reference $post which contains data like $post->post_title. You can get the title using get_the_title() and pass in the current $post within the loop.
    – kingkool68
    Commented Aug 25, 2016 at 1:53
  • Like so: $my_title = get_the_title( $post ); See developer.wordpress.org/reference/functions/get_the_title
    – kingkool68
    Commented Aug 25, 2016 at 1:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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