• Resolved marcallkz

    (@marcallkz)


    I’m trying to get my homepage (home.php) to display a few posts thumbnails from a custom post type and to paginate through all of them.

    Works OK for the first page, but when I try to get to the second page it doesn’t work.

    if I add the following code to my functions.php

    function homepage_custom( $query )
    {
        if ($query->is_home() && $query->is_main_query())
        {
            $query->set('post_type', array( 'custom' ));
        }
    }
    add_action( 'pre_get_posts', 'homepage_custom' );

    I get no posts.

    Trying again without the check for the main query

    function homepage_custom( $query )
    {
        if ($query->is_home())
        {
            $query->set('post_type', array( 'custom' ));
        }
    }
    add_action( 'pre_get_posts', 'homepage_custom' );

    I get the first posts (i’m displaying 16 on each page). When I go to the 2nd page the URL displays “localhost/page/2” but I see the same 16 posts. Reseting the permalinks to default gives me the same results.

    If I try querying directly from the home.php with this:

    $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    query_posts(array("post_type" => "custom", "paged" => $paged));

    I see the first 16 and I get 404 on the 2nd page. Again, reseting the permalinks gives the same result.

    I have 17 posts with this custom type.
    Displaying 16 each page I get 2 pages seeing the same posts, and displaying 5 each page I get 4 pages seeing the same posts.

Viewing 1 replies (of 1 total)
  • Thread Starter marcallkz

    (@marcallkz)

    Found it.

    In the same page I have to show two different custom post types. One post type is dislpayed on the main area, 16 per page. And the other is used to fill a list in the sidebar.

    Using this piece of code I change the main query

    function homepage_custom( $query )
    {
        if ($query->is_home() && $query->is_main_query())
        {
            $query->set('post_type', array( 'custom' ));
        }
    }
    add_action( 'pre_get_posts', 'homepage_custom' );

    but when I use the following in the home.php

    get_side_bar();
    The main query is somehow ignored, that’s why I get nothing with the check for the main query. Without the main query check, I’m always searching for my custom post type and it works for the first 16 posts and then the query from the side bar somehow breaks it in a way it loses the reference on what posts to show and I end up with the same no matter the page.

    Using wp_reset_query() method, removing this query from the sidebar.php or using WP_query instances where loops iterates through local variables solves the problem.

Viewing 1 replies (of 1 total)
  • The topic ‘WordPress pagination break on homepage’ is closed to new replies.