1

I've read a lot of questions about this topic. None of them solve my issue.

Poblem: pagination is shown at page 1 but page 2 returns 404.

The query and loop are in front-page.php:

// WP_Query arguments
$args = array(
  'post_type'              => 'trabajo',
  'posts_per_page'         => '2',
  'paged'                  => ( get_query_var('paged') ) ? get_query_var('paged') : 1,
);

// The Query
$trabajo_query = new WP_Query( $args );
$temp_query = $wp_query;
$wp_query   = NULL;
$wp_query   = $trabajo_query;


// The Loop
if ( have_posts() ) {
    while ( have_posts() ) {
        the_post();
        the_title();
    }
}
wp_reset_postdata();
the_posts_navigation();

$wp_query = NULL;
$wp_query = $temp_query;  // Reset
5
  • Why are you replacing the main query? Commented Feb 20, 2018 at 8:13
  • Because the_posts_navigation(); only works with main query. This is the normal procedure, as far as I know.
    – aitor
    Commented Feb 20, 2018 at 8:21
  • But why not just use the normal post type archive? Commented Feb 20, 2018 at 8:29
  • I need several CPTs for this work: works, news and bio. And I need to show two loops with works and news in the front page.
    – aitor
    Commented Feb 20, 2018 at 8:35
  • refer this answer How to fix pagination for custom loops?
    – DHL17
    Commented Feb 20, 2018 at 10:31

3 Answers 3

2

I found the final answer here: https://wordpress.stackexchange.com/a/217534/77722

Page 2 of front page was taking pagination from main query, not from my custom query.

I've taked these actions:

1. To change name of front-page.php to index.php in order to get the main query every time page is loaded (even when paginated)

2. To change main query with pre_get_posts in order to show posts of my CPT:

add_action( 'pre_get_posts', function ( $q ) {
    if (    $q->is_home() && $q->is_main_query() ) {
        $q->set( 'posts_per_page', 1 );
        $q->set( 'post_type', 'trabajo');
    }
});

3. Do a normal loop in the index.php:

if ( have_posts() ) {
    while ( have_posts() ) {
        the_post();
        the_title();
    }
}
wp_reset_postdata();
the_posts_navigation();

Works perfectly!

1

I found here a workaround: Custom post type pagination 404 fix?

Since I don't understand it, I don't mark this question as solved. Any explanation of this will be appreciated.

I must to do two actions together:

  1. Set 1 the post limit in the admin>reading dashboard.
  2. Add this function:

.

add_action( 'parse_query','changept' );
function changept() {
    if( !is_admin() )
        set_query_var( 'post_type', array( 'post', 'trabajo' ) );
    return;
}

It works, but I don't know what side effects will have this.

-2

Put this code in functions.php file

    function pagination($pages = '', $range = 1) {
        $showitems = ($range * 3) + 1;

        global $paged;
        if (empty($paged))
            $paged = 1;

        if ($pages == '') {
            global $wp_query;
            $pages = $wp_query->max_num_pages;
            if (!$pages) {
                $pages = 1;
            }
        }

        if (1 != $pages) {
            echo "<div class=\"gallery-pagination\">";
            echo '<ul class="pagination">';
//        if ($paged > 2 && $paged > $range + 1 && $showitems < $pages)
//            echo "<a href='" . get_pagenum_link(1) . "'>&laquo; First</a>";
            if ($paged > 1 && $showitems < $pages)
                echo "<a href='" . get_pagenum_link($paged - 1) . "'> << </a>";
//        echo '<ul class="pagination">';
            for ($i = 1; $i <= $pages; $i++) {

                if (1 != $pages && (!($i >= $paged + $range + 1 || $i <= $paged - $range - 1) || $pages <= $showitems )) {
                    echo ($paged == $i) ? "<li class='active'><a href='javascript:void(0)'>" . $i . "</a></li>" : "<li class='inactive'><a href='" . get_pagenum_link($i) . "'>" . $i . "</a></li>";
                }
            }

            if ($paged < $pages && $showitems < $pages)
                echo "<a href=\"" . get_pagenum_link($paged + 1) . "\"> >> </a>";
            echo '</ul>';

            echo "</div>\n";
        }
    }

Put this code in your custom post type page where you want it.

<?php
            // Get a list of categories
            $paged = ( get_query_var('paged') ) ? absint(get_query_var('paged')) : 1;
            $args = array(
                'post_type' => 'post_gallery',
                'posts_per_page' => 9,
                'order_by' => 'date',
                'order' => 'ASC',
                'paged' => $paged,
            );

            $new_query = new WP_Query($args);
//             print_r($new_query);
            ?>
<?php if (function_exists("pagination")) { ?>
                <?php pagination($new_query->max_num_pages); ?>
            <?php } ?>
1
  • This is not a useful answer, you should explain what changes this code has and how it solved the question asker's problem. Commented Feb 20, 2018 at 14:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.