简体   繁体   中英

Wordpress: Custom Post Type query not finding advanced custom field meta_query

I have a custom post type in Wordpress called People (each post in people is a person with some bio details etc).

Inside each People post I've created a custom field called 'tags' which has a series of checkboxes.

If someone clicks on the System Manager checkbox I want to be able to only pull through all the system managers on a particular page.

The most success I've had is using the wp_get_recent_posts instead of new wp_query .. this will output everybody UNTIL I add my extra meta query search. Then nothing appears.

$people_args = array(
    'post_type' => 'people',
    'post_status'=>'publish',
    'meta_query' => array(
    array(
        'key' => 'tags', // name of custom field
        'value' => 'systemmanager',
        'compare' => 'LIKE'
    )
    )
);
$people_posts = wp_get_recent_posts($people_args);
foreach( $people_posts as $people ){    

    $fields = get_fields($people["ID"]);

    if (get_post_thumbnail_id( $people["ID"] )>0) {
        $myimgsp = wp_get_attachment_image_src(get_post_thumbnail_id($people["ID"]),'full');
        echo '
            <li class="item">
                <a href="'.get_permalink(43).'"><img src="'.$myimgsp[0].'" alt="'.$people["post_title"].'" class="img-responsive" /></a>
                <div class="profile">
                    <h3>'.$people["post_title"].'</h3>
                    <p>'.$fields["job_title"].'</p>
                    <a href="'.get_permalink(43).'"  class="btn invert black">View Profile</a>
                </div>

            </li>';
    }
}
wp_reset_query();

I've been stuck on this for hours trying out all the different types of loops but I've had no luck. Has anyone come across this problem before?

If you got stuck on the query maybe try different approach and use the post_id from the query and make small loop inside your code there use get_post_meta(post_id) to check the meta key and get the tags value... something like this...

$meta = get_post_meta($post_ID);
foreach($meta as $key=>$val)
    {
        if ($key == 'tags')
        echo $val;
    }

So I'm feeling a little silly! In my functions.php I had forgotten to set that the custom post type should support custom fields. So even though my Advanced Custom Fields were showing .. it wasn't getting pulled through when I was calling it.

To rectify I added to my custom post type config:

'supports' => array('title','thumbnail','custom-fields')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM