• Hello,

    I try to make a custom pagination work.
    I put this in my functions.php

    function pagination_april( $query ) {
    if ( $query->monthnum=04&year=2005 ) {
    $query = new WP_Query( ‘posts_per_page=1’ );
    }
    }

    add_action( ‘pre_get_posts’, ‘pagination_april’ );

    But it won’t work.
    Can anyone tell me what went wrong here.

    Roelof

Viewing 11 replies - 1 through 11 (of 11 total)
  • Thread Starter roelof

    (@roelof)

    Oke,

    So it must be :

    function pagination_april( $query ) {
     if ( $query->monthnum==04&&year==2005 ) {
     $query = new WP_Query( 'posts_per_page=1' );
     }
     }
    
    add_action( 'pre_get_posts', 'pagination_april' );
    Thread Starter roelof

    (@roelof)

    Hello,

    I tried it but still no luck.
    Still all the articles are displayed.

    Anyone a idea ?

    Roelof

    try to treat year and month seperately in the if statement:

    function pagination_april( $query ) {
     if ( $query->monthnum==04 && $query->year==2005 ) {
     $query = new WP_Query( 'posts_per_page=1' );
     }
     }
    
    add_action( 'pre_get_posts', 'pagination_april' );
    Thread Starter roelof

    (@roelof)

    Hello,

    So i tried this :

    function pagination_april( $query ) {
     if ( $query->year==2005) {
        if ( $query->monthnum==04) {
     $query = new WP_Query( 'posts_per_page=1' );
     }
     }
    }
    
    add_action( 'pre_get_posts', 'pagination_april'

    But now I get a 500 error on my site.

    Roelof

    Thread Starter roelof

    (@roelof)

    Hello,

    The 500 error is solved.
    But still the pagination is not working.

    But I did some research and found out this.

    The variable month and year are still 0.
    But there is a variable m which contains 200504.

    So I changed it to this :

    function pagination_april( $query ) {
     if ( $query->m==200504) {
           $query = new WP_Query( 'posts_per_page=1' );
     }
     }
    
    add_action( 'pre_get_posts', 'pagination_april'

    But then again a 500 error.
    It seems that WordPress cannot do what I need.

    Roelof

    Moderator Helen Hou-Sandi

    (@helen)

    Core Lead Developer and 4.0, 4.7, and 5.6 Release Lead

    You shouldn’t need to set $query to a new WP_Query, but rather just do $query->set( 'posts_per_page', 1 );.

    Thread Starter roelof

    (@roelof)

    Thank you
    But I still get 500 error.

    Roelof

    Moderator Helen Hou-Sandi

    (@helen)

    Core Lead Developer and 4.0, 4.7, and 5.6 Release Lead

    What did you do before that got rid of the 500 error?

    This may not be the cause of the problem, but you might try:

    function pagination_april( $query ) {
        // in 3.3, use is_main_query() instead
        global $wp_the_query;
        if ( $query->m == 200504 && $query === $wp_the_query ) {
            $query->set( 'posts_per_page', 1 );
        }
     }
    
    add_action( 'pre_get_posts', 'pagination_april' );
    Thread Starter roelof

    (@roelof)

    I changed the /> to ?>

    Still a 500 error. It looks like WordPress does not like the use of the variable m .If I change it to month no 500 error but then the script does not work as expected.

    Roelof

    Thread Starter roelof

    (@roelof)

    Oke,

    I found out if I change the permalinks to this : https://test.tamarawobben.nl/2005/04/

    year and month have the good values of 2005 and 04.
    But still this is not workimg.

    function pagination_april( $query ) {
    
    if ( $query->year==2005) {
        if ( $query->monthnum==04) {
                  $query->set( 'posts_per_page', 1 );
    }
     }
    }
    
    add_action( 'pre_get_posts', 'pagination_april');

    Roelof

Viewing 11 replies - 1 through 11 (of 11 total)
  • The topic ‘custom pagination problem’ is closed to new replies.