繁体   English   中英

自定义字段查询多个帖子类型 - Wordpress

[英]Custom field query multiple post types - Wordpress

我试图查询数据的两个自定义帖子类型,以下不起作用。 我尝试了几个版本......但没有运气。

  $args = array(
    'post_type' => array('custom1', 'custom2'),
      'meta_query' => array(
      'relation' => 'AND',
      //the first two keys are custom fields found in custom1 custom posts
      array(
        'key' => 'firstKey',
        'value' => 3
      ),
      array(
        'key' => 'secondKey',
        'value' => cool
      ),
      //the third key can be found in custom2 custom posts
      array(
        'key' => 'thirdKey',
        'value' => 3
      )
      )
  );

根据您的意见:如果要对不同类型的用户使用自定义查询,则可能需要动态创建查询。

例如,如果“酷”类型的用户获得特殊待遇,那么基本想法将是:

$args = array( 'post_type' => 'custom' );
if (user is 'cool') {
    $args['meta_query'] = array(
         'relation' => 'AND',
         array(
             'key' => 'secondKey',
             'value' => 'cool'
         ),
         array(
             'key' => 'thirdKey',
             'value' => 3
         )
    };
} else {
    $args['meta-key'] = 'thirdKey';
    $args['meta_value'] = 3
}

这只是像WP_Query($ args)那样传递它,如果它是静态的那样。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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