Support

Account

Home Forums General Issues Query post object not working

Solved

Query post object not working

  • I don’t know what’s happening, but very recently I cannot seem to get a field in the current post and use it to query a post object.

    In the following code, I’m trying to get the fields class and playoff_game and query them in a post object. Does not work! Very frustrating.

    function my_post_object_query( $args, $field,$post_id ) {
    
    global $post;
    setup_postdata( $post ); 
    
    $post_id = $post->ID;
    $ID = $post->ID;	
    $class  = get_field('class',$post_id);
    
    $args = array(
       // 'meta_key' => 'game_date_2',// priority
       'orderby' => 'title',
       'order' => 'ASC',
       'post_status' => 'publish',
       'post_type' => 'game',
       'posts_per_page' => 20,
       'category_name' => 'boys-tennis',
       'meta_query' => array('relation' => 'AND',
    
            array(
                'key' => 'playoff_game',
                'value' => 1
                ),
    
            array(
                'key' => 'class',
                'value' => $class,//array('A North'),
                ),
    /*
            array('relation' => 'OR',
            		array(
                'key' => 'round',
                'value' => 'quarterfinal'
                ),
            		array(
                'key' => 'round',
                'value' => 'semifinal'
                ),
            		array(
                'key' => 'round',
                'value' => 'regional final'
                ),
            		array(
                'key' => 'round',
                'value' => 'state championship'
                )
    				),
    */
            ),
    
        );
    
        return $args;
    //else:
    //endif;    
    }
    
    // filter for every field
    // add_filter('acf/fields/post_object/query', 'my_post_object_query', 10, 3);
    
    // filter for a specific field based on it's name
    //add_filter('acf/fields/post_object/query/name=my_post_object', 'my_post_object_query', 10, 3);
    
    // filter for a specific field based on it's key
    add_filter('acf/fields/post_object/query/key=field_593060bd83376', 'my_post_object_query', 10, 3);
    
  • This part of your code is really unnecessary because the current post ID is passed to your filter

    
    global $post;
    setup_postdata( $post );
    
    $post_id = $post->ID;
    $ID = $post->ID;
    

    There are several other things that may or may not be going on.

    Can the field class have multiple selections? If it can then you’ll need to do a “LIKE” query on this field.

    If this field was altered and “Select multiple values” was changed then some or your posts may contain single values and some may contain serialized arrays. If this is the case then you’re going to have to do an “OR” query checking for both == and LIKE.

    What is the return format set to, Post Object or Post ID? If it’s returning a post object then you’ll need to extract the post ID from the object returned. This could also be effected by the multiple factor and could be returning an array of post object.

    What type of field is playoff_game? I’m guessing a true false field. Does this field exist for every existing post or was it added. Posts that do not have this field at all could be throwing off the query, although I’m not sure about this.

  • Thanks for the response John.

    I don’t think any of those points you made are an issue, because when I substitute a string from one of the choices in the class pulldown, the query works as expected.

    I understand that the $post_id defaults to the current post, but I don’t know if something got changed with an update. It’s just not getting the $post_id from the current page for me.

  • Is it an error in the post ID or in what is being returned by get_field that’s causing the issue?

    The filter you have runs during an AJAX call, so it may be hard for you to see what’s going on. You can write values to your error log and then look in there.

    
    function my_post_object_query( $args, $field, $post_id ) {
    
      $class  = get_field('class',$post_id);
    
      error_log($post_id);
      error_log($class);
    
      // ...
    
    
  • I will keep that error_log in mind for future projects. I got it to work by including $post_id in get_field() function.

    Here is the working code:

    function my_post_object_query( $args, $field, $post_id ) {
    
    $cat = get_the_category($post_id);
    $sport = $cat[0]->slug;
    
    /*
    echo '<pre>';
    	print_r($x['class']);
    echo '</pre>';
    */
    
    $round = get_field('round',$post_id);
    $division = get_field('class',$post_id);
    
    if($round == 'prelim'):
    	$next_round = 'quarterfinal';
    
    elseif($round == 'quarterfinal'):
    	$next_round = 'semifinal';
    
    elseif($round == 'semifinal'):
    	$next_round = 'regional final';
    
    elseif($round == 'regional final'):
    	$division = array('A','B','C','D');
    	$next_round = 'state_championship';
    
    else:
    	$division = array($division,'A','B','C','D');
    	$next_round = array('quarterfinal','semifinal','regional final','state championship');
    endif;	
    	
    
    $args = array(
       // 'meta_key' => 'game_date_2',// priority
       'orderby' => 'ID',
       'order' => 'ASC',
       'post_status' => 'publish',
       'post_type' => 'game',
       'posts_per_page' => 10,
       'category_name' => $sport,
       'meta_query' => array('relation' => 'AND',
    
            array(
                'key' => 'playoff_game',
                'value' => 1
                ),
    
             array('relation' => 'OR',
           array(
                'key' => 'class',
                'value' => $division,
                ),
    ),
            array('relation' => 'OR',
            		array(
                'key' => 'round',
                'value' => $next_round,//array('quarterfinal','semifinal','regional final','state hampionship'),
                ),
         	  ),
    
            ),
    
        );
    
        return $args;
     
    }
    
    // filter for every field
    // add_filter('acf/fields/post_object/query', 'my_post_object_query', 10, 3);
    
    // filter for a specific field based on it's name
    //add_filter('acf/fields/post_object/query/name=my_post_object', 'my_post_object_query', 10, 3);
    
    // filter for a specific field based on it's key
    add_filter('acf/fields/post_object/query/key=field_593060bd83376', 'my_post_object_query', 10, 3);
    
    

    This for your help with this!

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

The topic ‘Query post object not working’ is closed to new replies.