Limit posts on a WP_Query and in WooCommerce product query

折月煮酒 提交于 2019-12-20 03:38:07

问题


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

I know how to limit post in wp_query

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

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

How to do this?

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

That query is like: LIMIT 10 OFFSET 15

Please help me.


回答1:


The answer is… 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 );

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…

See this thread: Whats the difference between post_limits and pre_get_posts



来源:https://stackoverflow.com/questions/37191730/limit-posts-on-a-wp-query-and-in-woocommerce-product-query

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!