I have a custom post type called pasakumi and for some reason in wp-admin the views (All / Published / Trash) are showing only 4 random posts, even though I have more than that. They're also not working, for example, 'Trash (1)' is showing the same 4 posts as 'All' (see gif below). If I create a new post, it just replaces one of the 4 shown.
The following is the code from functions.php I have for the custom post type. Maybe I need to change something here?
add_action( 'pre_get_posts', function ( $query ) {
if ( is_post_type_archive( 'pasakumi' ) && $query->is_main_query() ) {
$query->set( 'post_type', 'pasakumi' );
$query->set( 'post_status', array( 'future', 'publish' ) );
$query->set( 'orderby', 'date' );
$query->set( 'order', 'ASC' );
$query->set( 'posts_per_page', '-1' );
$query->set( 'date_query', array( array( 'after' => array( 'year' => date( 'Y' ), 'month' => date( 'n', strtotime('last month') ), ), ) ) );
}
} );
$args = [
"label" => __( "Pasākumi", "custom-post-type-ui" ),
"labels" => $labels,
"description" => "",
"public" => true,
"publicly_queryable" => true,
"show_ui" => true,
"delete_with_user" => false,
"show_in_rest" => true,
"rest_base" => "",
"rest_controller_class" => "WP_REST_Posts_Controller",
"has_archive" => true,
"show_in_menu" => true,
"show_in_nav_menus" => true,
"delete_with_user" => false,
"exclude_from_search" => false,
"capability_type" => "post",
"map_meta_cap" => true,
"hierarchical" => false,
"rewrite" => [ "slug" => "pasakumi", "with_front" => true ],
"query_var" => true,
"menu_position" => 4,
"menu_icon" => "dashicons-post-status",
"supports" => [ "title", "editor", "custom-fields" ],
];
Any help will be appreciated!
I have a custom post type called pasakumi and for some reason in wp-admin the views (All / Published / Trash) are showing only 4 random posts, even though I have more than that. They're also not working, for example, 'Trash (1)' is showing the same 4 posts as 'All' (see gif below). If I create a new post, it just replaces one of the 4 shown.
The following is the code from functions.php I have for the custom post type. Maybe I need to change something here?
add_action( 'pre_get_posts', function ( $query ) {
if ( is_post_type_archive( 'pasakumi' ) && $query->is_main_query() ) {
$query->set( 'post_type', 'pasakumi' );
$query->set( 'post_status', array( 'future', 'publish' ) );
$query->set( 'orderby', 'date' );
$query->set( 'order', 'ASC' );
$query->set( 'posts_per_page', '-1' );
$query->set( 'date_query', array( array( 'after' => array( 'year' => date( 'Y' ), 'month' => date( 'n', strtotime('last month') ), ), ) ) );
}
} );
$args = [
"label" => __( "Pasākumi", "custom-post-type-ui" ),
"labels" => $labels,
"description" => "",
"public" => true,
"publicly_queryable" => true,
"show_ui" => true,
"delete_with_user" => false,
"show_in_rest" => true,
"rest_base" => "",
"rest_controller_class" => "WP_REST_Posts_Controller",
"has_archive" => true,
"show_in_menu" => true,
"show_in_nav_menus" => true,
"delete_with_user" => false,
"exclude_from_search" => false,
"capability_type" => "post",
"map_meta_cap" => true,
"hierarchical" => false,
"rewrite" => [ "slug" => "pasakumi", "with_front" => true ],
"query_var" => true,
"menu_position" => 4,
"menu_icon" => "dashicons-post-status",
"supports" => [ "title", "editor", "custom-fields" ],
];
Any help will be appreciated!
Share Improve this question edited Dec 4, 2019 at 22:52 dans_j asked Dec 4, 2019 at 11:09 dans_jdans_j 155 bronze badges 2 |1 Answer
Reset to default 1Here's your problem:
add_action( 'pre_get_posts', function ( $query ) {
if ( is_post_type_archive( 'pasakumi' ) && $query->is_main_query() ) {
This is great on the frontend! And the backend!! pre_get_posts
works for all queries, nothing about it is exclusive to the frontend, and you can filter queries on the backend too. As a result, your date query gets applied to every single query, even the admin ones
So first, check if it's the admin, aka:
if ( is_admin() ) {
return;
}
Second, you don't need to put everything inside that giant if statement, just check for the opposite and return early.
Third, this is awful for performance:
$query->set( 'posts_per_page', '-1' );
Set the value high, an unrealistically high value, but never set it to -1
. Sure it might work for you now, but what happens when the business pivots and the number of pasakumi sky rockets? Or in 10 years time after lots of posts get created? Or when there's a lot of visitors to the site?
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744938757a4602189.html
pre_get_posts
hook in there? – kero Commented Dec 4, 2019 at 12:05