doutang1873 2019-03-16 19:47
浏览 83

Wordpress分页错误与查询字符串

I have a page with custom WP Query to output different posts related to the award post type.

$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
$args = array(
    'posts_per_page'        => 2,
    'ignore_sticky_posts'   => 1,
    'post_type'             => 'award',
    'post_status'           => 'publish',
    'orderby'               => 'date',
    'order'                 => 'DESC',
    's'                     => $_GET['keyword'],
    'paged'                 => $paged;
);

I am using the default wordpress pagination function like

paginate_links( array(
    'base'         => str_replace( 999999999, '%#%', esc_url( get_pagenum_link( 999999999 ) ) ),
    'total'        => $query->max_num_pages,
    'current'      => $paged,
    'format'       => '?paged=%#%',
    'show_all'     => false,
    'type'         => 'plain',
    'mid_size'     => 3,
    'prev_next'    => false,
    'add_args'     => false,
    'add_fragment' => '',
));

If I submit this url awards/?keyword=test, I get relevant results with few page numbers. If I click on page 2 on the pagination, the page redirects to this url awards/page/2/?keyword=test and I get a page not found error! I get the same error if I visit awards/page/2/! What am I doing wrong?

It would be also good if I can retain my query strings in the url like such awards/?page=2&keyword=test.

UPDATE

If delete the award custom post type but I keep the posts in the database, the pagination with the filters work as they should.

Also, the WP query is within a page called awards with a slug of awards.

My award custom post type:

$cpt = array(
      'singular' => 'Article',
      'plural'   => 'Awards & Press',
      'type'     => 'award',
      'slug'     => 'awards'
  );

  register_post_type( $cpt['type'],
    array(
      'labels'             => array(
        'name'               => sprintf( '%s', $cpt['plural'] ),
        'singular_name'      => sprintf( '%s', $cpt['singular'] ),
        'add_new'            => 'Add New',
        'add_new_item'       => sprintf( 'Add New %s', $cpt['singular'] ),
        'edit'               => 'Edit',
        'edit_item'          => sprintf( 'Edit %s', $cpt['singular'] ),
        'new_item'           => sprintf( 'New %s', $cpt['singular'] ),
        'all_items'          => sprintf( 'All %s', $cpt['plural'] ),
        'view'               => sprintf( 'View %s', $cpt['singular'] ),
        'search_items'       => sprintf( 'Search %s', $cpt['plural'] ),
        'not_found'          => sprintf( 'No %s Found', $cpt['plural'] ),
        'not_found_in_trash' => sprintf( 'No %s Found In Trash', $cpt['plural'] ),
        'parent_item_colon'  => ''
      ),
      'description'        => 'Create a new item',
      'public'             => true,
      'show_ui'            => true,
      'show_in_menu'       => true,
      'publicly_queryable' => true,
      'has_archive'        => false,
      'rewrite'            => array(
        'slug' => $cpt['slug'],
        'with_front' => false,
      ),
      'menu_position'      => 5,
      'show_in_nav_menus'  => true,
      'menu_icon'          => 'dashicons-admin-post',
      'hierarchical'       => false,
      'query_var'          => true,
      'supports'           => array(
        'title',
        'editor',
        'excerpt',
        'thumbnail',
      )
    )
  );

ANOTHER UPDATE

If I change:

'rewrite'            => array(
            'slug' => $cpt['slug'],
            'with_front' => false,
          ),

To

'rewrite'            => false,

The pagination with filters work, however I lose the pretty url awards/post-name/ and it becomes awards/?award=post-name.

I need to have the pagination with filters working while maintaining the pretty urls for the posts.

  • 写回答

1条回答 默认 最新

  • doujianmin6527 2019-03-17 00:26
    关注

    I believe I found the answer, but if someone else can confirm this to be the best solution I would be greatful:

    add_action('init', function() {
      add_rewrite_rule('(.?.+?)/page/?([0-9]{1,})/?$', 'index.php?pagename=$matches[1]&paged=$matches[2]', 'top');
    });
    

    I added the code above to the functions.php file.

    Solution was taken from here: https://www.grzegorowski.com/wordpress-rewrite-rules/

    评论

报告相同问题?

悬赏问题

  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记