0

I'm adding a custom dropdown filter to the admin cpt posts view.

I'm hooking in the action 'restrict_manage_posts' with the following function:

add_action('restrict_manage_posts', 'show_filters' );

function show_filters() {

    $tag = isset( $_GET[ 'partner' ] ) ? esc_attr( $_GET[ 'partner' ] ) : null;

    $args = array(
        'post_type' => 'partner',
    );

    $partners_query = new WP_Query( $args );

    $partners_options = array( '' => 'Choose partner' );

    if ( ! empty( $partners_query->posts ) ) {
        foreach ( $partners_query->posts as $partner ) {
            $partners_options[$partner->ID] = $partner->post_title;
        }
    } else {
        // Just for check that the query is returning empty
        $partners_options = array( '' => 'EMPTY QUERY: Debugging purposes' );
    }       

    if ( ! empty( $partners_options ) ) {

        echo '<select name="partner" class="postform">';

        foreach ( $partners_options as $id => $name ) {
            if( $name ) {
                printf('<option value="%s"%s>%s</option>', esc_attr( $id ), selected( $id, $tag, false), esc_html( $name ) );
            }
        }

        echo '</select>';
    }

}

EDIT: I have a pre_get_posts filter, that adds the meta_query to the filtering:

add_action( 'pre_get_posts', 'orderby_filtering' );
function orderby_filtering( $query ) {
    if ( isset( $_GET[ 'partner' ] ) && $_GET[ 'partner' ] ) {
        $query->query_vars['meta_query'][] = array(
             'key'      => 'partner_select',
             'value'    => (int)$_GET[' partner ']
        );
    }
}

The filter works well, and it returns the correct values, but once executed, the new dropdown shows the "EMPTY QUERY" message instead of the partners list that should appear.

Why the query is not executed again? What I'm missing? Thanks!

  • I'm not sure what the problem is because when I try your code the only way I get the "EMPTY QUERY" is where there are NO posts of that post type. Could you have anything hooked into the WP_Query-related actions/filters (e.g., pre_get_posts that is altering the query IF isset( $_GET[ 'partner' ] ) == true? – Paul 'Sparrow Hawk' Biron Mar 14 '17 at 16:38
  • I will give a try. Note that the "EMPTY QUERY" appears when I make some filtering choosing one value from the dropdown, not in the initial posts list screen, with no elements filtered at the moment. – Capiedge Mar 14 '17 at 17:02
  • @Paul'SparrowHawk'Biron you were right! And I solve the problem, adding $query->is_main_query() to the conditional. If you want to publish the answer I will vote it as correct. Thanks. – Capiedge Mar 14 '17 at 19:49
  • we've all been there. glad to help. – Paul 'Sparrow Hawk' Biron Mar 14 '17 at 20:19
0

You've probably hooked into one of the WP_Query-related actions/filters (e.g., pre_get_posts) and altering the query when isset( $_GET[ 'partner' ] ) == true. Find a way to conditionally avoid that modification (e.g., checking if $query->is_main_query()).

|improve this answer|||||

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.