0

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

3 Answers 3

1

You might try

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

to stop the sticky posts from being prepended.

2
  • 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! Commented Jun 27, 2013 at 17:20
  • ok great you got it resolved, thanks for the voting.
    – birgire
    Commented Jun 27, 2013 at 19:55
0

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.

2
  • 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. Commented Jun 27, 2013 at 17:19
  • Got it. I owe you a beer. Commented Jun 27, 2013 at 17:24
0

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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