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.