Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Filter for Post Status #21

Open
ingageco opened this issue Jul 26, 2016 · 3 comments
Open

Filter for Post Status #21

ingageco opened this issue Jul 26, 2016 · 3 comments

Comments

@ingageco
Copy link

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

@jtsternberg
Copy link
Member

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' );
}

@ingageco
Copy link
Author

Justin - this is great. Thank you!

@jtsternberg
Copy link
Member

👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants