CMB2-Post-Search-field
CMB2-Post-Search-field copied to clipboard
Filter for Post Status
Any possibility of filtering based on status? It would be nice to eliminate drafts in my current use case. Thanks!
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' );
}
Justin - this is great. Thank you!
👍