0

I have many user contactmethods on my Profile page. If the user has not completed them all, then every time they log in they are supposed to be redirected to a "Reminder" post. But I want to hide this Post everywhere else. I have a pre_get_posts function which performs this action: $query->set( 'post__not_in', array( 639 ) ); which does a fine job of hiding Post number 639 everywhere. The problem is that when I execute the login_redirect filter (which verifies that the required fields are finished), and it finds that the user's profile is incomplete, the redirect to the "Reminder" post results in a 404-Not Found error (because it has been excluded from WP_query - duh).

I understand that I'm causing my own problem, but I don't know how to get out of it. How can I hide a Post everywhere except for when it is called specifically?

4
  • I think you need to change where your "hiding code" is executed. Can you change it so that it executes only after the WP_Query is initialised, so you can wrap it in an if that says "if the current page isn't 639, then hide 639"? – Paul G. Jun 14 at 21:43
  • That would require a completely different approach. I'm blocking access to this Post everywhere with this add_action( 'pre_get_posts', 'ETSm_function' ); function ETSm_function($query) { $query->set( 'post__not_in', array( 639 ) ); } My approach using post__not_in would have to be replaced entirely if I wanted to "execute only after the WP_Query is initialized," and I'm willing to do that, but I don't know what method to use instead. Might be better to set the $query argument conditionally, I preceded it with if (!$wp_query->is_singular() ) but that throws a fatal error. – eLIANT Jun 14 at 22:25
  • Maybe if I look up the calling PHP program, which I believe is login.php, as a condition of setting the $query argument...?? – eLIANT Jun 14 at 22:29
  • I tried using PHP function like this: ` if(str_contains( $_SERVER['HTTP_REFERER'], "/wp-login.php")) $query->set( 'post__not_in', array( 639 ) );` but I get another error - "undefined function". How can a native PHP function be undefined? – eLIANT Jun 14 at 22:45
0

********** I think I have it *********** you can't interrogate WP_query like I was trying to do. You must work with the $query parameter's values like this: if( !$query->is_single() ) $query->set( 'post__not_in', array( 639 ) ); } So if it's not a single Post, like in a list of Recent Posts or a list of posts in a Category, it won't show. But if it IS a single post on display, then it appears.

1
  • FINAL (I hope) UPDATE......... As it is, my answer will also hide the Post from the Admin>Posts list, which means you won't be able to edit it via the list. To avoid this, use this line in your pre_get_post function: ` if( !$query->is_single() && !is_admin()) $query->set( 'post__not_in', array( 639 ) );` notice that both conditions are negative. – eLIANT Jun 15 at 19:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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