In my own plugin I have a custom post type faq. In the admin listing are 14 faq posts. In the Wordpress Settings under Reading is 10 posts per pages set. Additionally, I have added a pre_get_posts function with the following content.
function hep_pre_get_posts_faq(WP_Query $query)
{
// check if current query is not the main query
if (!$query->is_main_query()) {
return;
}
// edit the query only when post type is 'faq'
if (!is_admin() || $query->query['post_type'] == 'hep_faq') {
return;
}
// Protect against arbitrary paged values
$paged = (get_query_var('paged')) ? absint(get_query_var('paged')) : 1;
// set pagination
$query->set('posts_per_page', 10);
$query->set('paged', $paged);
}
The list is displayed correctly with 10 entries. Where the pagination links are, there is written 14 items without pagination links. And this for a good reason, because if I force the display you see 1 of 1. But here should be 1 of 2 because there are 14 posts.
This already happened to me with another cpt. The strange thing here is that everything works fine on the live-system on the server. Only on the local development-system it doesn't display the pagination links because of the same reason as described above.
Of course this confuses me a lot. As it seems not to be the code, because it's exactly the same on the development system as on the live system.
I would be very happy for any help concerning this. The plugin is already live and is used productively by my customers.
Here are all query parameters:
Request
/wp-admin/edit.php
?post_type=hep_faq
Query String
post_type=hep_faq
&posts_per_page=20
Query Vars
cache_results 1
comments_per_page 50
lazy_load_term_meta 1
order DESC
paged 1
post_type hep_faq
posts_per_page 10
update_post_meta_cache 1
update_post_term_cache 1
and the custom post type arguments:
$args = array(
'label' => 'faq',
'description' => 'FAQ',
'labels' => $labels,
'supports' => array('title'),
'hierarchical' => false,
'capabilities' => $caps,
'public' => false,
'show_ui' => true,
'show_in_nav_menus' => false,
'show_in_menu' => 'hep-dashboard',
'show_in_admin_bar' => true,
'menu_position' => 1,
'menu_icon' => plugins_url() . "/img/contact.png",
'can_export' => true,
'has_archive' => true,
'exclude_from_search' => true,
'publicly_queryable' => true,
'capability_type' => array('hep_faq', 'hep_faqs'),
'map_meta_cap' => true,
'query_var' => true,
'rewrite' => array('slug' => get_option('hep_faq_slug')),
);
In my own plugin I have a custom post type faq. In the admin listing are 14 faq posts. In the Wordpress Settings under Reading is 10 posts per pages set. Additionally, I have added a pre_get_posts function with the following content.
function hep_pre_get_posts_faq(WP_Query $query)
{
// check if current query is not the main query
if (!$query->is_main_query()) {
return;
}
// edit the query only when post type is 'faq'
if (!is_admin() || $query->query['post_type'] == 'hep_faq') {
return;
}
// Protect against arbitrary paged values
$paged = (get_query_var('paged')) ? absint(get_query_var('paged')) : 1;
// set pagination
$query->set('posts_per_page', 10);
$query->set('paged', $paged);
}
The list is displayed correctly with 10 entries. Where the pagination links are, there is written 14 items without pagination links. And this for a good reason, because if I force the display you see 1 of 1. But here should be 1 of 2 because there are 14 posts.
This already happened to me with another cpt. The strange thing here is that everything works fine on the live-system on the server. Only on the local development-system it doesn't display the pagination links because of the same reason as described above.
Of course this confuses me a lot. As it seems not to be the code, because it's exactly the same on the development system as on the live system.
I would be very happy for any help concerning this. The plugin is already live and is used productively by my customers.
Here are all query parameters:
Request
/wp-admin/edit.php
?post_type=hep_faq
Query String
post_type=hep_faq
&posts_per_page=20
Query Vars
cache_results 1
comments_per_page 50
lazy_load_term_meta 1
order DESC
paged 1
post_type hep_faq
posts_per_page 10
update_post_meta_cache 1
update_post_term_cache 1
and the custom post type arguments:
$args = array(
'label' => 'faq',
'description' => 'FAQ',
'labels' => $labels,
'supports' => array('title'),
'hierarchical' => false,
'capabilities' => $caps,
'public' => false,
'show_ui' => true,
'show_in_nav_menus' => false,
'show_in_menu' => 'hep-dashboard',
'show_in_admin_bar' => true,
'menu_position' => 1,
'menu_icon' => plugins_url() . "/img/contact.png",
'can_export' => true,
'has_archive' => true,
'exclude_from_search' => true,
'publicly_queryable' => true,
'capability_type' => array('hep_faq', 'hep_faqs'),
'map_meta_cap' => true,
'query_var' => true,
'rewrite' => array('slug' => get_option('hep_faq_slug')),
);
Share
Improve this question
asked May 13, 2019 at 15:56
GogoGogo
234 bronze badges
1 Answer
Reset to default 1"Posts per pages" limit from "Settings -> Reading" does not apply here (post list in the dashboard) and default size of list is 20.
Your pagination does not work properly because you use pre_get_posts
action hook to change the per_page limit, which is not the right way. The per_page limit overwritten by pre_get_posts
filter will only work in some situations (it will not work, for example, for hierarchical types) and it will affect number of posts fetched from DB, but in pagination original per_page value will be used.
Posts per page in dashboard
To change the number of posts per page for the lists in a dashboard, use hooks:
- edit_posts_per_page
- edit_{$post_type}_per_page
add_filter('edit_hep_faq_per_page', 'se337791_hepfaq_posts_per_page');
function se337791_hepfaq_posts_per_page( $posts_per_page )
{
return 10;
}
// -- OR --
add_filter('edit_posts_per_page', 'se337791_posts_per_page', 20, 2);
function se337791_hepfaq_posts_per_page( $posts_per_page, $post_type )
{
if ( 'hep_faq' == $post_type )
return 10;
return $posts_per_page;
}