限制 WP_Query 和 WooCommerce 产品查询中的帖子 [英] Limit posts on a WP_Query and in WooCommerce product query

查看:63
本文介绍了限制 WP_Query 和 WooCommerce 产品查询中的帖子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 Woocommerce 中,我需要使用 wp_query 限制帖子.

In Woocommerce, I need to limit post with a wp_query.

我知道如何限制 wp_query

例如,如果这个页面 id 是 20,我正在运行查询:

For example if this page id is 20, I am running the query:

function wpcodex_filter_main_search_post_limits( $limit, $query ) {

    if ( get_the_ID()==20 ) {
        return 'LIMIT 0, 12';
    }

    return $limit;
}


add_filter( 'post_limits', 'wpcodex_filter_main_search_post_limits', 10, 2 );

但我需要将限制设置为 25 到 35.

But I need to set the limt as 25 to 35.

如何做到这一点?

即使我放置 return 'LIMIT 25, 35'; 它仍然显示前 35 个产品,而不是 25 到 35 个之间的帖子.

Even if I am placing return 'LIMIT 25, 35'; it is still showing first 35 products, not the posts between 25 to 35.

该查询类似于:LIMIT 10 OFFSET 15

请帮帮我.

推荐答案

答案是…… taratata!!! :

function wpcodex_filter_main_search_post_limits( $limit, $query ) {
    if ( get_the_ID()==20 ) {
        return 'LIMIT 25, 10'; 
    }
    return $limit;
}
add_filter( 'post_limits', 'wpcodex_filter_main_search_post_limits', 10, 2 );

因为在'LIMIT 25, 10';中,这里25是偏移量,10是要显示的帖子数.这样它就会显示 10 个产品,从产品 26 开始......

Because in 'LIMIT 25, 10';, here 25 is the offset and 10 the number of posts to display. So that way it will display 10 products beginning with the product 26…

查看此线程:有什么区别post_limits 和 pre_get_posts 之间

这篇关于限制 WP_Query 和 WooCommerce 产品查询中的帖子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆