filters - Having issue with dynamic data within restrict_manage_posts function

admin2025-06-05  7

I have the dropdown dynamically populated with the titles of my podcast post type and I can successfully apply a filter by selected a podcast title and clicking filter. However, when the page loads and I see my filtered list the dynamic dropdown does not get populated.

Any idea why this is happening?

Here's my code pulled from my ThemeHelper class...

add_filter( 'parse_query', [ __CLASS__, 'parse_query' ] );
add_action( 'restrict_manage_posts', [ __CLASS__, 'restrict_manage_posts' ] );

function parse_query( $query ) {
    global $pagenow;
    // Change query based on post_parent
    $parent_filter = $_GET['parent'];
    if ( is_admin() && $pagenow == 'edit.php' && ! empty( $parent_filter ) ) {
        $query->query_vars['post_parent'] = (int) $parent_filter;
    }
}

public static function restrict_manage_posts() {

    if ( isset( $_GET['post_type'] ) && $_GET['post_type'] == 'episode' ) {
        $args     = [
            'post_type'   => 'podcast',
            'numberposts' => - 1,
        ];
        $podcasts = get_posts( $args );

        $select = '<select name="parent"><option value="">Podcast (' . count( $podcasts ) . ')</option>';
        foreach ( $podcasts as $podcast ) {
            $selected = ( isset( $_GET['parent'] ) && (int) $_GET['parent'] > 0 ) ? 'selected="selected"' : '';
            $select   .= '<option value="' . $podcast->ID . '" ' . $selected . '>' . $podcast->post_title . '</option>';
        }
        $select .= '</select>';
        echo $select;
    } else {
        return;
    }
}
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1749100909a316368.html

最新回复(0)