一个GET_POSTS,为每个元值返回多个自定义帖子

时间:2014-07-15 作者:vadims

我设置了一个带有自定义字段的自定义贴子“product”cf_type. 我想要一个get_posts 这将返回总共20种产品,其中10种产品cf_type: fog 和10个cf_type: gof. 这可能吗?

$args = array(
    \'post_type\' => \'product\',
    \'meta_query\' => array(
        array(
            \'key\' => \'cf_type\',
            \'value\' => \'fog\',   // need to have 10 with value \'fog\' and 10 with value \'gof\'
        )
    ),
    \'numberposts\' => 10,
    \'orderby\' => \'modified\',
    \'order\' => \'desc\'
 );
$prodslist = get_posts( $args );`

1 个回复
SO网友:HU ist Sebastian

简短回答:没有。

但是,如果您想对所有需要的帖子使用一个循环,可以这样做:

$args = array(
    \'post_type\' => \'product\',
    \'meta_query\' => array(
        array(
            \'key\' => \'cf_type\',
            \'value\' => \'fog\',   // need to have 10 with value \'fog\' and 10 with value \'gof\'
        )
    ),
    \'numberposts\' => 10,
    \'orderby\' => \'modified\',
    \'order\' => \'desc\',
    \'fields\' => \'ids\'
 );
$postsfog = get_posts($args);
$args[\'meta_query\'] = array(array(\'key\' => \'cf_type\',\'value\' => \'gof\');
$postsgof = get_posts($args);
$myloopposts = get_posts(array(
                  \'posts_per_page\' => -1,
                  \'posts__in\' => array_merge($postsfog,$postsgof)),
                  \'post_type\' => \'product\',
                  \'orderby\' => \'post__in\',
                  \'ignore_sticky_posts\' => true
              ));

结束

相关推荐