Support

Account

Home Forums General Issues Querying by Post Object value

Solved

Querying by Post Object value

  • Hello – I need some help with a specific WP query using the Post Object field.

    I’m building a site for a musician who plays in several projects (CPT: Project). Each event (CPT: Event) she adds to her site offers a field in which she can specify the project performing (Post Object field).

    I am running queries which display this information and which order the events by date (Date Picker field) (splitting into two lists – upcoming and past). On individual project pages though I want to filter out events for display (still in date order) so that only event posts with the current project are displayed.

    I have been playing with this tutorial but have not been able to make it work yet.

    The basic loop (sorted by date but showing all upcoming events regardless of project) is as follows:

    <?php 
    	//Set server timezone to GMT
    	date_default_timezone_set('Europe/London'); 
    	//Today's date
    	$date_1 = date('Ymd', strtotime("now")); 
    	//Future date - the arg will look between today's date and this future date to see if the post fall within the 2 dates
    	$date_2 = date('Ymd', strtotime("+48 months"));
    ?>
    <?php
     $upcoming_args = array(
      'post_type' => 'Event',
      'meta_query' => array(	  	
       array(
        'key' => 'event_date',
        'compare' => 'BETWEEN',
        'type' => 'DATE',
        'value' => array($date_1, $date_2),
       )
      ),
     'orderby' => 'meta_value_num',
     'order' => 'ASC',
     'nopaging' => true
     );
    ?>	
    <?php $upcoming_query = new WP_Query( $upcoming_args ); ?>

    Is is possible to run two meta-queries together, and to order the results by one, then the next?

    And is it possible to do something like:

    array (
     'key' => 'artist', 
     'value' => '"' . get_the_ID() . '"', 
     'compare' => 'LIKE'
    )

    How can I do this? Thanks.

  • I am still struggling with this. After more reading, experimenting, testing etc I have come up with this set of args for the query I am trying to create, but still with no output.

    <?php 
     //Set server timezone to GMT
     date_default_timezone_set('Europe/London'); 
     //Today's date
     $date_1 = date('Ymd', strtotime("now")); 
     //Future date - the arg will look between today's date and this future date to see if the post fall within the 2 dates
     $date_2 = date('Ymd', strtotime("+48 months"));
    ?>
    
    <?php
     $byartist_args = (array(
      'numberposts' => -1,
      'post_type' => 'Event',
      'meta_query' => array(
      'relation' => 'AND',
      array(
       'key' => 'artist',
       'value' => '"' . get_the_ID() . '"',
       'compare' => 'LIKE',
      ),
      array(
       'key' => 'event_date',
       'compare' => 'BETWEEN',
       'type' => 'DATE',
       'value' => array($date_1, $date_2),
       ),
      ),
     ));
    ?>	
    <?php $byartist_query = new WP_Query( $byartist_args ); ?>

    I can only assume that the issue is with this part, as everything else seems to be working okay:

    'value' => '"' . get_the_ID() . '"',

    There must be a way to compare the current post ID with the ID in a post object field and output based on this. Anyone?

  • The query you are running would show only the posts assigned to this artist if done correctly. If you’re post object field only allows a single value then that value is not stored as a serialized array. What are the settings of the post object field “artist”? Does it allow multiple values? If it does then your meta query should be correct, if it only allows one value then your query should be

    
    array(
       'key' => 'artist',
       'value' => get_the_ID(),
       'compare' => '=',
      ),
    
  • John, this is great, thank you (as always). It worked exactly as you suggested.

    I am now trying, however, to make sure I can still present them in date order (the other meta query). I’ve been using this to do that in other feeds:

    'orderby' => 'meta_value_num',
    'order' => 'ASC',
    'nopaging' => true

    Of course, now there are two meta queries, there are two meta values too, so how can I specify which is to be used for ordering? At the moment it seems a bit confused.

    Here’s the whole thing as it stands currently:

    $byartist_args = (array(
     'numberposts' => -1,
     'post_type' => 'Event',
     'meta_query' => array(
      'relation' => 'AND',
      array(
       'key' => 'artist',
       'value' => get_the_ID(),
       'compare' => '=',
      ),
      array(
       'key' => 'event_date',
       'compare' => 'BETWEEN',
       'type' => 'DATE',
       'value' => array($date_1, $date_2),
      ),
     ),
     'orderby' => 'meta_value_num',
     'order' => 'ASC',
     'nopaging' => true
    ));

    Thank you!

  • If you are still ordering by a single meta field you can still to that without a lot of work. I think this will work.

    
    $byartist_args = (array(
     'numberposts' => -1,
     'post_type' => 'Event',
     'meta_query' => array(
      'relation' => 'AND',
      array(
       'key' => 'artist',
       'value' => get_the_ID(),
       'compare' => '=',
      ),
      array(
       'key' => 'event_date',
       'compare' => 'BETWEEN',
       'type' => 'DATE',
       'value' => array($date_1, $date_2),
      ),
     ),
     'meta_key' => 'event_date'
     'orderby' => 'meta_value_num',
     'order' => 'ASC',
     'nopaging' => true
    ));
    
  • Bingo. Thank you so much, once again. I didn’t realise it was possible to reference that event_date field a second time to initiate the sorting. Always learning in this job.

    I really appreciate the help.

Viewing 6 posts - 1 through 6 (of 6 total)

The topic ‘Querying by Post Object value’ is closed to new replies.