Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

How would I go about hiding a post based on a meta_value selection done on the back end.

The usual method is to just change the post status from Publish to Draft however, I want this to work based on a dropdown selection.

meta_key being current_status and a dropdown box with meta_value of active and leased.

If active is selected the post is shown, and leased would hide the post.

Any ideas on how to do this in a clean way?

share|improve this question

2 Answers 2

up vote 1 down vote accepted

Use the pre_get_posts action to alter the main query with meta_query arguments to only select posts with active current_status.

This example would work for your main posts page. See Conditional Tags for how to determine when other types of queries are run.

function wpa_current_status( $query ) {
    if ( $query->is_home() && $query->is_main_query() ) {
        $meta_query = array(
            array(
                'key' => 'current_status',
                'value' => 'active',
                'compare' => '='
            )
        );
        $query->set( 'meta_query', $meta_query );
    }
}
add_action( 'pre_get_posts', 'wpa_current_status' );

EDIT- or within a custom query, set meta_query arguments directly:

$args = array(
    'post_type' => 'property',
    'posts_per_page' => -1,
    'meta_query' => array(
        array(
            'key' => 'current_status',
            'value' => 'active',
            'compare' => '='
        )
    )
);

$properties = new WP_Query( $args );
share|improve this answer
    
I can't get this to work with posts made through a custom page. It still shows the posts even with is_page. To add some more information, these posts are going within a function property_listing() and I'm adding individual property's and selecting whether they are active or leased via current_status. –  Nima Moradi Apr 2 '13 at 2:19
    
if you are creating a custom query via WP_Query and not altering the main query, then just add the meta_query parameters directly to your query. –  Milo Apr 2 '13 at 2:44
    
I -almost- have this working. I decided to alter main query as the results are all over the site and not just specific pages. In order to see what areas it's working on, I used if ( $query->is_main_query() ) {. It works in the backend where I can see and edit posts as it will filter to only show those with active status, but in the front end it returns 404 errors with nothing found and unable to filter. Thank you by the way for helping me with this. –  Nima Moradi Apr 2 '13 at 3:26
    
it's not going to work on the main query of a single page, it has to be an archive of some sort- the main posts page, a category, a custom post type archive, etc., or otherwise you need to create an additional query. –  Milo Apr 2 '13 at 3:29
    
I tried using if ( $query->is_page( 'search-results' ) && $query->is_main_query() ) { so that it uses a specific page for the results, and the same occurs. Nothing shows. I'm going to have to do some digging as I'm thinking the current_status value isn't properly being linked to posts. Not sure what else would cause it to not give a return. –  Nima Moradi Apr 2 '13 at 3:40

Within the loop, extract the value.

Once you have it, do a simple if test to see if it's leased. If it is, you just do a continue via PHP.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.