Support » Developing with WordPress » Setting the properties of paginate_links() cannot override the max post setting

  • Resolved eadwig



    I managed to control the number of posts to display on an archive post page using the code below:

    $args = array( ‘post_type’ => ‘post’,
    ‘posts_per_page’ => 10,
    ‘category_name’ => ‘NEWS’,
    ‘paged’ => $paged,

    However, upon clicking the Next Page link in the pagination, the browser returns ERROR 404. The code for the pagination is as follows:

    echo paginate_links( array( ‘format’ => ‘page/%#%’, ‘current’ => $paged, ‘total’ => $the_query->max_num_pages, ‘mid_size’ => 2, ‘prev_text’ => __(‘« Prev Page’), ‘next_text’ => __(‘Next Page »’) ) );

    ERROR 404 mentioned above is resolved only and if only the max number of posts to display is set to below the value of ‘posts_per_page’ on the WP admin screen, which I found inconvenient because I want to show different numbers of posts on paginated pages across different archive post pages.

    Is there a way to override the WP’s max number of posts to display using paginate_links()?

Viewing 5 replies - 1 through 5 (of 5 total)
  • Not that way. The pagination goes with the original query. If you filter the original query, there’s nothing to do on paginate_links.

    Read the code of

    Moderator bcworkz


    To filter the original query, use the “pre_get_postsaction (not filter). Set the desired query_vars similar to defining custom WP_Query arguments, except you use WP_Query::set() method instead of defining an array of arguments.

    Thank you, both, for the advice. I read the code of the plugin and the page on pre_get_posts, which helped me understand that $query->set() is used to modify the number of posts to display for the original main query. However, the use of the code as shown below just hid my pagination:

    function my_home_category( $query ) {
    if ( $query->is_home() && $query->is_main_query() ) {
    $query->set( ‘posts_per_page’, 2 );
    add_action( ‘pre_get_posts’, ‘my_home_category’ );

    Is this to do with me not using WP_Query::set()? Is there an example of calling WP_Query::set() to achieve the purpose?

    Moderator bcworkz


    WP_Query::set() is the scope resolution, static form of class methods. It’s the standard way to refer to class methods in documentation. In practical use we normally use a variable containing a class instance with the object operator to reference class methods and properties, so $query->set(). Both forms refer to the same thing, each has a specific situation for its use in practice. The object operator -> is far more common, but requires us to know what variable holds the class object instance. Without knowing that, the scope resolution form :: is more convenient and unambiguous as a generic reference in discussions and documentation.

    Probably the reason your paginate_links() call fails now is the $the_query variable passed has no value. Neither does $paged. In order to pass the correct values, you need to access the global query object. If you declare global $wp_query;, you can then access the max_num_pages property ($wp_query->max_num_pages)and paged query var ($wp_query->get('paged')).

    While paginate_links() will still work when passed the correct values, because you’ve modified the main query, you can instead use typical template tags for pagination like get_the_posts_navigation() or any of the functions listed under Pagination in the Codex.

    Pay attention to singular and plural forms of “post” in function names. Singular “post” (get_the_post_navigation()) is for use on single post templates. Plural “posts” is for use with archive type template lists of multiple posts.

    BTW, when you post code in these forums, please demarcate with backticks or use the code button. If you don’t, your code becomes corrupted by the forum’s parser and becomes unusable in testing or copying for modification and use.

    I tried possible working solutions again and again over the weekend. Adding a hook in the format below turned out to be the only solution that works:

    add_action(‘pre_get_posts’, function ($q){
    if (is_category()){
    $q->set( ‘posts_per_page’, 2 );

    • This reply was modified 1 year, 4 months ago by eadwig.
Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Setting the properties of paginate_links() cannot override the max post setting’ is closed to new replies.