从某个ID开始的循环

时间:2014-03-05 作者:Greeso

是否可以从某个post ID开始循环?我希望所有的帖子都从某个ID开始,体面地。为了解释,我必须先做出一些假设。

假设我的WordPress数据库有20篇文章。显然,每个帖子都有一个ID,并假设这些帖子的ID范围为1到20。

还假设当我们通过循环加载post时,默认情况下使用的是所有内容。因此,循环将从20到1检索记录/帖子。如果范围限制为10,则第1页将检索20到11,第2页将检索10到1。

现在,回到我的问题上来。假设我想接收记录17到8。我是否可以通过某种方式将17传递给循环,并从17到8检索10条记录?这可能吗?

以下是有助于理解问题的几行代码:

<?php 
    $args = array(
        \'post_type\'          => \'post\',
        \'post_status\'        => \'publish\',
        \'category_name\'      => \'news\',
        \'posts_per_page\'     => 10,

        \'ID_VARIABLE_HERE\'   => 17 // I would like to be able to pass the ID, and the loop should return 10 posts (as defined in the posts_per_page), descendingly, starting at 17. 

    );



    $custom_query = new WP_Query($args);

?>  

<?php if ($custom_query->have_posts()): while ($custom_query->have_posts()) : $custom_query->the_post() ; ?>

    <article class="my-article">

        <div class="my-image"><?php the_post_thumbnail(); ?></div>

        <div class="my-title"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></div>

        <div class="my-details"><?php the_excerpt(); ?></div>

    </article>

<?php endwhile; else: ?>
    <p><?php _e(\'No contents exist.\'); ?></p>
<?php endif; ?>
正如你所看到的,我想通过一个论点$args, 将具有ID值(上例中为17)。阅读上面的代码注释,了解所需的详细信息。有可能吗?如果不是通过中的参数$args, 另一种方法是什么?

谢谢

4 个回复
SO网友:Rajilesh Panoli

您可以使用posts\\u where hook来实现这一点

add_filter( \'posts_where\' , \'posts_where\' );

function posts_where( $where ) {

    global $wpdb;

    $start = $query->get(\'ID_VARIABLE_HERE\');
    // add any condition if you have
    if ( ! is_admin() && $query->is_main_query()){

        $where .= " AND {$wpdb->posts}.ID >= $start";
    }
    return $where;
}

SO网友:Stephen S.

将使用offset 参数是否满足您的需要?

$args = array(
    \'post_type\'          => \'post\',
    \'post_status\'        => \'publish\',
    \'category_name\'      => \'news\',
    \'posts_per_page\'     => 10, 
    \'offset\'             => 17
);

SO网友:ghoul

您可以执行以下操作,提供要以$max\\u post\\u id开头的post id,并向下递减到可以以$min\\u post\\u id提供的特定post\\u id:

// Get all post ids.
$all_post_ids = get_posts(array(
    \'fields\'          => \'ids\', // Only get post IDs
    \'posts_per_page\'  => -1
));

// Make a list of all possible post ids between two post ids.
$max_post_id = 17;
$min_post_id = 8;
$custom_post_id = [];
for ( $i = $max_post_id; $i > $min_post_id; $i-- ) {
    $custom_post_id[] = $i;
}

// Filter out the valid post ids from the possible post ids.
$query_post_ids = array_intersect( $custom_post_id, $all_post_ids );

$args = array(
    \'post_type\'      => \'post\',
    \'post_status\'    => \'publish\',
    \'category_name\'  => \'news\',
    \'posts_per_page\' => 10,
    \'post__in\'       => $query_post_ids // Use the valid post ids in the array.
);

$custom_query = new WP_Query( $args );

if ( $custom_query->have_posts() ):
    while ($custom_query->have_posts()) :
        $custom_query->the_post() ; ?>

        <article class="my-article">

            <div class="my-image"><?php the_post_thumbnail(); ?></div>

            <div class="my-title"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></div>

            <div class="my-details"><?php the_excerpt(); ?></div>

        </article>

    <?php
    endwhile;
else: ?>
    <p><?php _e(\'No contents exist.\'); ?></p>
<?php endif;
请注意,这可能会产生性能问题,因为如果max和min之间的差距太大,我们将使用“for”循环创建递减为1的可能id。

SO网友:Elex

有一种方法pre_get_postsWP_Query 使用自定义参数!:)

假设这是我们的$args.

$args = array(
    \'post_type\'          => \'post\',
    \'post_status\'        => \'publish\',
    \'category_name\'      => \'news\',
    \'posts_per_page\'     => 10,

    \'maximum_id\'         => 17, // I choose this name for example, feel free
);
你必须抓住maximum_id 中的参数pre_get_posts

function wpse_136861_pre_get_posts($query) {
    if(intval($query->get(\'maximum_id\')) > 0 && $query->get(\'maximum_id\') >= $query->get(\'posts_per_page\'))
    {
        $start = $query->get(\'maximum_id\') - $query->get(\'posts_per_page\');

        // Be sure to avoid the negative / 0 value
        if($start <= 0)
        {
            $start = 1;
        }

        $end = $query->get(\'maximum_id\');

        $query->set(\'post__in\', range($start, $end)); // Set your post_id array with all int beetween $start and $end (7-17 here)
        $query->set(\'orderby\', \'ID\'); // Set by ID DESC (DESC order is default in WP_Query)
    }
    return $query;
}
add_action(\'pre_get_posts\', \'wpse_136861_pre_get_posts\');
这项工作:)

结束

相关推荐

更改用户ID的起始号

我有一个客户希望他们的在线用户注册与其当前数据库相匹配。因此,他们希望他们的wordpress注册从00100开始,我似乎找不到一个允许这样做的插件,有没有调整数据库来实现这一点?