简体   繁体   中英

WP_Query + $query_vars + custom field

I have custom post type items in my theme. They represent projects in portfolio. I have a custom field checkbox that sets the option "Featured" for every single project. So, when the project is featured, it's being shown on the main page of the site. If the "Featured" checkbox is unset, the project is visible only on the portfolio page. What I need now is to query only featured projects to show them on the main page. I use this code to show the posts but I don't know how to add the option to get only posts with the "Featured" custom field set to True.

$args = array(
        'post_type' => 'project',
        'post_status' => 'publish');
    $projects = new WP_Query($args);
    if ($projects->have_posts()) {
        while ($projects->have_posts())
... Show the project info here

What can I do about this?

Add this to your array.

   meta_key => 'featured',
   meta_value => 'true'

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