Support

Account

Home Forums General Issues Issue using post_object field value in WP_Query

Solved

Issue using post_object field value in WP_Query

  • Hello! I’m having an issue attempting to retrieve some posts using WP_Query. I’ve spent a few hours trawling through the Internet, but not had a lot of luck.

    Just a little background:

    I’m attempting to retrieve posts that have an ACF field called show_venue, with a value of the title of the current page. show_venue is a post_object field type.

    I’ve arrived at the following:

    $args = array(
    	'numberposts' => -1,
    	'post_type' => 'post',
    	'meta_key' => 'show_venue',
    	'meta_key_value' => get_the_title(), // "Venue one"
    	'order' => 'ASC'
    );

    Then it’s run by WP_Query.

    I’m getting three posts back. Two of them are correctly “Venue one”, but a third post with a value of “Venue two” is appearing. Only these three posts have the field value set.

    I’d really appreciate any pointers on where I’m going wrong, I just can’t seem to figure it out.

    Thanks for your time! 🙂
    – Callum Kerr

  • try:

    
    $args = array(
    	'numberposts' => -1,
    	'post_type' => 'post',
    	'meta_key' => 'show_venue',
            // meta_key_value should be meta_value
    	'meta_value' => get_the_title(), // "Venue one"
    	'order' => 'ASC'
    );
    
  • Hey, thanks for your suggestion. Changing the field name returned no posts at all I’m afraid. Just in case, I tried hardcoding the value instead of get_the_title(), but it was unsuccessful too.

  • I missed something extremely important in your OP, the field is a post object field

    
    $args = array(
    	'numberposts' => -1,
    	'post_type' => 'post',
    	'meta_key' => 'show_venue',
    	'meta_value' => $post->ID, // post objects store the post ID
    	'order' => 'ASC'
    );
    
  • EDIT: I made a mistake, your $post->ID suggestion fixed the issue! I forgot to change the post type after I made some changes to the post type holding these details yesterday evening, that’s why it didn’t work.

    Thanks a million for your help, I really appreciate it. 🙂

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

The topic ‘Issue using post_object field value in WP_Query’ is closed to new replies.