CMB2-Post-Search-field icon indicating copy to clipboard operation
CMB2-Post-Search-field copied to clipboard

Filter for Post Status

Open ingageco opened this issue 9 years ago • 3 comments

Any possibility of filtering based on status? It would be nice to eliminate drafts in my current use case. Thanks!

ingageco avatar Jul 26 '16 04:07 ingageco

Sure, you could mimic what the plugin is doing w/ the post-types:

/**
 * Hook into pre_get_posts if we're doing a cmb2_post_search ajax request.
 */
function cmb2_post_search_check_if_ajax_post_search() {
    if (
        defined( 'DOING_AJAX' )
        && DOING_AJAX
        && isset( $_POST['cmb2_post_search'], $_POST['action'] )
        && 'find_posts' == $_POST['action']
    ) {
        add_action( 'pre_get_posts', 'cmb2_post_search_set_post_status' );
    }
}
add_action( 'admin_init', 'cmb2_post_search_check_if_ajax_post_search' );

/**
 * Set the post status via pre_get_posts
 * @param  array $query  The query instance
 */
function cmb2_post_search_set_post_status( $query ) {
    $query->set( 'post_status', 'publish' );
}

jtsternberg avatar Jul 27 '16 16:07 jtsternberg

Justin - this is great. Thank you!

ingageco avatar Jul 27 '16 16:07 ingageco

👍

jtsternberg avatar Jul 27 '16 16:07 jtsternberg