N
N
Nikolay2018-10-05 11:31:54
WordPress
Nikolay, 2018-10-05 11:31:54

Why is Wordpress pagination adding a post_name resulting in a 404 error?

Hello!
In general, everything is a bit confusing:
We have a page /articles/with a working pagination, and a page /multimedia/on which we get a 404 error when going to the pagination page.
If you copy the entire code from /articles/to /multimedia/- then /multimedia/pagination still does not work :) Some kind of magic.
I noticed that on the working page the request looks like this:

SELECT wp_posts.* FROM wp_posts WHERE 1=1 AND (wp_posts.ID = '43') AND wp_posts.post_type = 'page' ORDER BY wp_posts.post_date DESC

And where pagination does not work, like this:
SELECT wp_posts.* FROM wp_posts WHERE 1=1 AND wp_posts.post_name = 'page' AND wp_posts.post_type = 'multimedia' ORDER BY wp_posts.post_date DESC

That is, for some reason, a condition is added to the request. wp_posts.post_name = 'page'
The code is the same everywhere, and comes down to this form: (I tried not to overwrite the global variable - it didn’t help)
$global_wp_query = $wp_query;
$paged = get_query_var('paged') ? absint(get_query_var('paged')) : 1;
$wp_query = new WP_Query([
    'posts_per_page' => 15,
    'post_type' => 'post', // В мультимедиа здесь - multimedia.
// Пробовал добавлять это в запрос - ничего не менялось.
//    'post_name' => '', 
//    'name' => '',
    'paged' => $paged,
]);

while ($wp_query->have_posts()) : $wp_query->the_post();
// Манипуляции с выводом превью
endwhile; wp_reset_postdata();

// Ну и вывод самой пагинации.
the_posts_pagination([
    'total'     => $wp_query->max_num_pages,
    'mid_size'  => 3,
    'prev_next' => true,
    'prev_text' => '←',
    'next_text' => '→',
]); $wp_query = $global_wp_query

No functions.phpmanipulations with pagination were made, except for changing the appearance:
add_filter('navigation_markup_template', function ($template, $class) {
    return
        '<nav class="navigation %1$s" role="navigation">'.
        '    <div class="nav-links">%3$s</div>'.
        '</nav>';
}, 10, 2);

"Settings permalinks" reset; Google did not help, any advice about the number of posts in the settings and others - does not help at all.
Well, it’s very interesting why the same code on one page ( /articles/) works correctly, but /multimedia/not on the other ( ) :)

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question