WP_Query + $ query_vars +自定义字段

问题描述:

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?

我的主题中有自定义的帖子类型项目。 它们代表投资组合中的项目 我有一个自定义字段复选框,为每个项目设置选项“精选”。 因此,当项目出现时,它将显示在网站的主页上。 如果未设置“特色”复选框,则项目仅在组合页面上可见。 我现在需要的是仅查询特色项目以在主页面上显示它们。 我使用此代码显示帖子但我不知道 我不知道如何添加选项以仅获取“精选”自定义字段设置为True的帖子。 p>

  $ args = array(
'post_type'=>  'project',
'post_status'=>'publish'); 
 $ projects = new WP_Query($ args); 
 if($ projects-> have_posts()){
 while($ projects-  > have_posts())
 ...在此处显示项目信息
  code>  pre> 
 
 

我该怎么办? p> div>

Add this to your array.

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