多种职位类型,但仅设置一种职位

问题描述

我正在显示多种帖子类型,但我想为其中一种帖子类型设置偏移量,但是我该怎么做?

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

解决方法

您可以收集不需要的ID。如果您不需要带有偏移量的帖子类型的特定订单,而只是从该类型中查找5个最新帖子。您可以执行以下操作:

<?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....