Home>

A list of custom posts (events) is output to the top in Wordpress.

The source is written as follows.

<? php
    $paged = get_query_var ('paged')? get_query_var ('paged'): 1;
    $args = array (
        'paged' =>$paged,
        'post_type' =>'events',
        'posts_per_page' =>4,
    );
    $toppage_query = new WP_Query ($args);
    ?>
    <? php if ($toppage_query->have_posts ()):?>
    <? php while ($toppage_query->have_posts ()): $toppage_query->the_post ();?>
,<h3><a href = "<? Php the_permalink ();?>"><? Php the_title ();?></a></h3>
    <? php endwhile;wp_reset_postdata ();?>
    <? php else:?>
    <? php endif;?>
    <? php if (function_exists ('wp_pagenavi')) wp_pagenavi (array ('query' =>$toppage_query));?>

The pagination is displayed using the plug-in "wp_pagenavi".
Pagination is displayed normally and matches the number of submissions, but if you click 2, 3, 4 or next, it will be 404. (URL is/page/2 /)

I saw a variety of online literature,
(1) Return the Palmer link to the default (/? Paged = 2).
②Posts_per_page'=>Align the number 4 with the number of "maximum number of posts to be displayed on one page".
(3) In the case of wp_pagenavi, instead of aligning as in (2), reduce the number of "maximum number of posts displayed per page".

I tried

and all three, but no matter what I do, it remains 404 ...

How can I solve this phenomenon?
Could someone please professor?

  • Answer # 1

    It is recommended to rewrite with pre_get_posts. WP_Query can be executed or paged on the template, but it is a waste of resources because the main query is executed once when the template is called. pre_get_posts is a method to rewrite before executing the main query.

    【【wordpress】 Would you like to use pre_get_posts? ]
    https://qiita.com/_ruka_/items/e14280d34eddf49efad1#pre_get_posts suggestions

    [Control main query with pre_get_posts | Tips Note by TAM]

Related articles