• Resolved cadcobrachicken

    (@cadcobrachicken)


    Earlier you brilliantly helped me display multiple custom post types under 1 admin menu (https://ww.wp.xz.cn/support/topic/multiple-custom-post-types-under-one-admin-menu/, https://ww.wp.xz.cn/support/topic/list-all-posts-of-all-post-types-in-a-single-admin-interface/).

    Which I implemented with this code in the functions.php file:

    <?php
    function all_posts_from_post_types( $query ) {
            if ( ! is_admin() ) {
                    return;
            }
    
            if ( ! $query->is_main_query() ) {
                    return;
            }
    
            if ( isset( $_GET['post_type'] ) && 'show_all' === $_GET['post_type'] ) {
                            $query->set(
                                    'post_type', [ 'post_type_1'
                                                    , 'post_type_2'
                                                    , 'post_type_3'
                                                    , 'post_type_4'
                                            ]
                            );
            }
    }
    add_action( 'pre_get_posts', 'all_posts_from_post_types' );
    ?>

    When I try to search from this newly created admin page, I get a WordPress error Invalid post type.
    This is because the search button is redirecting to URL below (note post_type=Array):
    https://mysite.com/wp-admin/edit.php?s=Sample+Search+String&post_status=all&post_type=Array

    If I manually change the URL to the below (note change of post_type=show_all), I to get search results.
    https://mysite.com/wp-admin/edit.php?s=Sample+Search+String&post_status=all&post_type=show_all

    Do you have any suggestions on how to make the ‘Search’ button work so users do not have to manually change the URL? How can I make the search button redirect to post_type=show_all instead of post_type=Array?

    Thank you

Viewing 7 replies - 1 through 7 (of 7 total)
  • Plugin Contributor Michael Beckwith

    (@tw2113)

    The BenchPresser

    Honestly not sure with that, especially since the way we got the one screen listing everything isn’t exactly conventional in the first place.

    Thread Starter cadcobrachicken

    (@cadcobrachicken)

    Thanks for the reply. It makes me feel better that you can’t think of a solution, I’ve been racking my brain for a couple weeks on this one without any progress. If you think of anything I would be pleased to hear about it.

    Plugin Contributor Michael Beckwith

    (@tw2113)

    The BenchPresser

    Best ideas I have is if by chance the buttons are filterable or the arguments that the generate are where you could implode the array it’s ending up trying to pass, into a comma-separated string.

    Thread Starter cadcobrachicken

    (@cadcobrachicken)

    I am not understanding your suggestion.

    Plugin Contributor Michael Beckwith

    (@tw2113)

    The BenchPresser

    This would be digging into the WordPress Admin core code, around how the buttons are created and crafted and what they end up doing. This is not me saying there’s for sure a way with this, just that that’s where I’d end up searching first.

    Thread Starter cadcobrachicken

    (@cadcobrachicken)

    That is clear, thank you, but I do not think I want to go the route of editing the WordPress core code.

    Plugin Contributor Michael Beckwith

    (@tw2113)

    The BenchPresser

    Nor was I suggesting that much, but much like we have the pre_get_posts hook that we used originally, there may be hooks and filters inside core that could safely be used to amend the button behavior. I don’t know for sure, and I wager you don’t either. That’s all I was getting at.

Viewing 7 replies - 1 through 7 (of 7 total)

The topic ‘Searching multiple custom post types under one admin menu’ is closed to new replies.