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.

I have a filter that filters posts from the main loop depending on a user role (handled externally)

The meta data is entered when adding a post/page.

This is the filter - it works for filtering posts from the main page loops and the search pages but sticky posts are ignored and will show regardless of user role. Are these not included in the main_query?

add_filter( 'pre_get_posts', 'hide_private_posts' );

function hide_private_posts( $query ) {

$loggedInArray  = array(
                  'key' => 'smartPrivate',
                  'value' => 'smartPrivate_loggedIn',
                  'compare' => '!='
            );
$mentorArray    = array(
                  'key' => 'smartPrivate',
                  'value' => 'smartPrivate_loggedInMentors',
                  'compare' => '!='
            );

// If user isn't logged in, hide all private posts / pages
if( !is_user_logged_in() ) {

    if ( $query->is_main_query() || $query->is_feed() ) {

        $query->set( 'meta_query', array(

            $loggedInArray,
            $mentorArray

        ));
    }

// If user is logged in but a learner, show logged in posts / pages
} else if( is_user_sso_role( array('learner') ) ) {

    if ( $query->is_main_query() || $query->is_feed() ) {

        $query->set( 'meta_query', array(

            $mentorArray

        ));
    }

}

return $query;
}
share|improve this question
add comment

3 Answers

up vote 1 down vote accepted

You might try

$query->set( 'ignore_sticky_posts', 1 );

to stop the sticky posts from being prepended.

share|improve this answer
    
Thanks birgire - marked as resolved as your response came through first. Not sure if that's the etiquette but I'm new around here. Thanks! –  rob_was_taken Jun 27 '13 at 17:20
    
ok great you got it resolved, thanks for the voting. –  birgire Jun 27 '13 at 19:55
add comment

This is the filter - it works for filtering posts from the main page loops and the search pages but sticky posts are ignored and will show regardless of user role. Are these not included in the main_query?

Your question is confusing but I am taking it to mean that "sticky posts" are showing up where you don't want them to.

Showing sticky posts is the default. WordPress will (nearly?) always juggle the sticky posts to the top unless explicitly told otherwise. To me, this logic is backwards. There are only a couple of place where sticky posts make sense, yet WordPress will move them to the top for nearly everything. My take is that 'ignore_sticky_posts' => false should be the default. The "exception" should not be "every case but one (or two)".

If that is the problem you are facing, you will have to add $query->set( 'ignore_sticky_posts', true ); to your filter. You will find yourself writing that a lot. I add it, or its equivalent-- 'ignore_sticky_posts' => true--, to virtually every query I write.

share|improve this answer
    
Sorry for the confusion, I was typing whilst smacking my head on the desk. Thanks for the explanation - I'll give that a go. If I'm honest I'm not sure exactly where to put it but I'll find a place ;) Thanks for your time - Rob. –  rob_was_taken Jun 27 '13 at 17:19
    
Got it. I owe you a beer. –  rob_was_taken Jun 27 '13 at 17:24
add comment

OK...

The fix above works for the meta_query filtering to work on these posts but stops them being 'sticky'.

Does anyone know a way I can filter them using the meta_query but still keep the stickies at the top?

Thanks in advance

Rob

share|improve this answer
add comment

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.