0

I want to show my custom post type live-blog in the all post listing page ie. http://www.wordpress.com:8080/wp-admin/edit.php

So for that i am using pre_get_posts hook in order to show live-blog and posts together on that page.

function show_custom_post_type_in_listing($query) {
    if ( is_admin() && $query->is_main_query() ) {
        if ( $query->is_home() || $query->is_archive() || $query->is_feed() ) {
            $query->set('post_type', array('post', 'live-blog'));
        }
    }
}
add_action('pre_get_posts', 'show_custom_post_type_in_listing');

This works well in listing the live-blog in all posts section but it breaks when i filter the posts on the basis of category.

My URL becomes

http://www.wordpress.com:8080/wp-admin/edit.php?s=&post_status=all&post_type=Array&_wpnonce=52e69e14a7&_wp_http_referer=%2Fwp-admin%2Fedit.php%3Fs%26post_status%3Dall%26post_type%3Dpost%26action%3D-1%26m%3D0%26cat%3D15%26post_format%26seo_filter%26readability_filter%26filter_action%3DFilter%26paged%3D1%26action2%3D-1&action=-1&m=0&cat=15&post_format=&seo_filter=&readability_filter=&filter_action=Filter&paged=1&action2=-1

Here post_type is set to Array which gives invalid post type.

Is there any way to fix this

2 Answers 2

0

It seems pretty hacky, but this does work in my testing (I tested with built-in page post type):

// Change the post list and search query.
add_action( 'pre_get_posts', static function ( $query ) {
    if ( ! is_admin() || ! $query->is_main_query() ) {
        return;
    }

    if ( ! in_array( $query->get( 'post_type' ), array( 'post', 'Array' ) ) ) {
        return;
    }

    $query->set( 'post_type', array( 'post', 'page' ) );
} );

// If `post_type` is set to `Array`, change back to `post` to satisfy core validation.
add_action( 'admin_init', static function () {
    if ( ! isset( $_GET['s'] ) || '/wp-admin/edit.php' !== $_SERVER['PHP_SELF'] || ! isset( $_GET['post_type'] ) || 'Array' !== $_GET['post_type'] ) {
        return;
    }

    $_GET['post_type'] = 'post';
    $_REQUEST['post_type'] = 'post';
} );

The main functional piece here is checking the $_GET['post_type'] value on the admin_init action, and if it's equal to Array, then change it to post (to satisfy WP core). Then when the query is actually constructed, change the post_type parameter to both post types.

8
  • It really works like a magic but whenever i use seo plugin like rank or yoast and i filter through the SEO scores it works well for the first time but gives invalid post type for second time Commented Jun 20, 2023 at 16:33
  • Yeah, that's the problem: you can't possibly support every plugin in existence. Some plugins might give you an option to adjust their queries, but not all.
    – Caleb
    Commented Jun 20, 2023 at 16:45
  • Is there any way just to provide rank math and yoast Commented Jun 20, 2023 at 17:01
  • You'll have to look into those plugins and see what options they provide for their sorting/searching.
    – Caleb
    Commented Jun 20, 2023 at 17:01
  • Can you please provide some help into it Commented Jun 20, 2023 at 17:02
-1

The problem is that you're setting the post_type query var to an array. While this works for most query purposes, it's not compatible with the admin post filtering links.

The pre_get_posts hook affects all queries on the site, not just the main query. By checking is_main_query(), you are only modifying the main query, but the filtering in the admin happens in a secondary query.

What you could do is further limit the scope of your pre_get_posts modification. You might check if the query is an admin query, and then further check if the post type is 'post'.

Here's an updated version of your function that should work:

function show_custom_post_type_in_listing($query) {
    if ( is_admin() && $query->is_main_query() && $query->get('post_type') === 'post' ) {
        $query->set('post_type', array('post', 'live-blog'));
    }
}
add_action('pre_get_posts', 'show_custom_post_type_in_listing');

This will only modify the main query on the post list in the admin when the post type is 'post', adding your custom post type to the query.

Remember to refresh your permalinks by visiting SettingsPermalinks in your WordPress dashboard after making changes to the query structure.

1
  • As far as I can tell this doesn't improve anything: the search still breaks. The explanation is correct, but the new code has the same problem.
    – Caleb
    Commented Jun 13, 2023 at 18:13

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.