2

I'm planing a custom post type functionality for my theme and need to be able to display more than just one post_type in a admin overview table. That request I imagine might look like edit.php?post_type[]=theme_slide_nivo&post_type[]=theme_slide_other. Haven't actually tried that but i'm pretty confident it won't work. So guys, is it possible to achieve such result without hacking core?

1
  • You should never have to hack to core. What exactly are you trying to accomplish, editing 2 custom post types at the same time?
    – Wyck
    Mar 24, 2011 at 20:00

2 Answers 2

2

I tried it out to get an idea of the problems you will run into. The following code allows you to specify multiple post types with the multi_post_type parameter:

add_action( 'pre_get_posts', 'wpse12970_pre_get_posts' );
function wpse12970_pre_get_posts( &$wp_query )
{
    if ( is_admin() && array_key_exists( 'multi_post_type', $_GET ) ) {
        $wp_query->set( 'post_type', $_GET['multi_post_type'] );
        add_filter( 'the_posts', 'wpse12970_the_posts', 10, 2 );
    }
}

function wpse12970_the_posts( $posts, &$wp_query )
{
    $wp_query->set( 'post_type', $GLOBALS['post_type'] );
    return $posts;
}

The first problem was that the global $post_type should be a single type, otherwise other functions break. So we "smuggle" the multiple post types in under another name, and remove them again after the query ran.

The counter at the top of the list and the custom columns are based on only one post type. If there are many results paging will probably break.

If you want to do this, you should create your own list class, a child of WP_List_Table, like WP_Posts_List_Table but then for multiple post types. Because edit.php loads this table by default and I see no way to intercept it, you should create your own replacement of edit.php in your plugin and use that. I think it is doable, and probably interesting, but a lot of work.

0

Just picking up the comment above and let you know that I was able to use that code to filter multiple post types with it, simply replace:

$wp_query->set( 'post_type', $_GET['multi_post_type'] );

with

$wp_query->set( 'post_type', explode(',',$_GET['multi_post_type']) );

Simply use the URL with a comma-separated string like &multi_post_type=page,post

1
  • The other way to do that would be to use array-type query string parameters e.g. &multi_post_type[]=page&multi_post_type[]=post, then WordPress will automatically make an array. Which I think is is what OP is doing in the question.
    – Rup
    Jun 26 at 10:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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