Ad

Multiple Post Types But Set The Offset Only For One

- 1 answer

I am displaying multiple post types but i want to set a offset for one of those post type but how can i do that?

$args = array(
    'post_type' => array('wins', 'memes'),
    'posts_per_page' => '5',
    //'offset' => '1', (with this i set the offset for both but i only want to set it for one of them.)
    'post_status' => 'publish'
);
Ad

Answer

You could gather the IDs that you do not want. If you don't have a specific order needed from the post type with the offset, and are just looking for the 5 most recents posts from that type. You can do the following:

<?php

$posts_to_exclude = array();

$args = array(

  'post_type' => 'wins', // post type you want to offset
  'numberposts' => 5 // the default is 5, but you can add for good measure

);

$posts = get_posts( $args );

if ($posts) {

  foreach ($posts as $post) {

    $posts_to_exclude[] = $post->ID;

  }

}

$args = array(

    'post_type' => array('wins', 'memes'),
    'posts_per_page' => '5',
    'post__not_in' => $posts_to_exclude,
    'post_status' => 'publish'

);

new WP_Query( $args );

// Do more stuff....
Ad
source: stackoverflow.com
Ad