1

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.

enter image description here

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!

2
  • 2
    Try disabling all plugins except the one with the CPT, does this still happen? Do you have some additional logic like a pre_get_posts hook in there?
    – kero
    Dec 4, 2019 at 12:05
  • @kero yes, I have pre_get_posts for the page, why would it be interfering with the admin page? (added the code in the main post)
    – dans_j
    Dec 4, 2019 at 22:53

1 Answer 1

1

Here'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?

2
  • Thanks! The return code worked. I was aware of post_per_page being -1, it's set that way just for the development process.
    – dans_j
    Dec 5, 2019 at 10:33
  • 1
    Set it to 500 or something silly, why give yourself extra work of going through the code hunting for -1 values :)
    – Tom J Nowell
    Dec 5, 2019 at 11:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.