最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

wp query - Multiple WP_Query args combinations according to post type

programmeradmin3浏览0评论

I'd like to have separate meta_query arguments according to the post type.

For example, I'd like to query t-shirts that have a green collar or dress-shirts that have a green inner-lining, right now I only do a single post type check where I check for t-shirts and dress-shirts and I separately check for the meta values which mean that it will also show different combinations such as t-shirts that have a green inner-lining

$query = new WP_Query(array(
    'post_type' => array('t-shirts', 'dress-shirts'),
    'meta_query' => array(
      'relation' => 'OR',
      array(
        'key' => 'collar',
        'value' => 'green',
      ),
      array(
        'key' => 'inner-lining',
        'value' => 'green',
      )
    ),
));

How can I set it up to work with two different args, one if the post type is t-shirts and a seperate args if the post type is dress-shirts which would look something like:

$query = new WP_Query(
  array(
    array(
      'post_type' => array('t-shirts'),
      'meta_query' => array(
        array(
          'key' => 'heal',
          'value' => 'green',
        )
      ),
    ),
    array(
      'post_type' => array('dress-shirts'),
      'meta_query' => array(
        array(
          'key' => 'sleeve',
          'value' => 'green',
        )
      ),
    ),
  )
);

Doing two separate queries won't work as it will mess up pagination.

发布评论

评论列表(0)

  1. 暂无评论