0

I have setup a custom post 'product' with a custom field cf_type. I wish to have one get_posts that will return 20 total products, 10 products with cf_type: fog and 10 with cf_type: gof. Is that possible?

$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 Answer 1

0

Short answer: no.

But if you want to use one loop for all your needed posts, you can do something like this:

$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
              ));

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.