I've been struggling with this for several hours. I want to have a post hidden from every listing, including the posts edit screen, and having it not reachable by browsing to it.

I successfully did this with the pre_get_posts action hook, but I need to be able to make the post reachable again only when I'm running a specific function, where I use cURL to request the post and do some validations, then I want to hide it again as soon as I'm done with it.

The problem I'm having, is that cURL gets a Not Found response when my pre_get_posts hook is active. I have tried using a *remove_action( 'pre_get_posts', 'my_function' )* statement, but it seems it's not taking it and I can't get to the post.

I thought of having the post just be a private post, or password protected, but I really prefer to have this post hidden completely.

I also tried the posts_where filter, same thing, it works but I can't make cURL get the post correctly.

Those are the only two hooks I have found that do what I want, but for some reason I cannot remove them.

Can anyone please share some ideas on how to make this work? Or, what am I missing?


Edit: Ok, here's the code I have for the pre_get_posts hook:

function hide_post( $query ) {
    $query->query_vars['post__not_in'] = array( $hidden_post_id ) );

And this is the code I use to show/hide the post:

remove_action( 'pre_get_posts', 'hide_post' );
$post_permalink = get_permalink( $hidden_post_id );

$ch = curl_init();
curl_setopt( $ch, CURLOPT_URL, $post_permalink );
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1 );
curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, 0 );
curl_setopt( $ch, CURLOPT_SSL_VERIFYHOST, 0 );

$buffer = curl_exec( $ch );
curl_close( $ch );

remove_action( 'pre_get_posts', 'hide_post' );

Worth noting that the part that's not working as I would expect is the remove_action() part, where I should be able to get to my hidden post and after that I will hide it again.

  • please edit the question and add the actual code that doesn't work. – Mark Kaplun Mar 4 '14 at 8:35
  • Did you try using the cURL with a specific parameter? You could just activate the hide_post-function if the parameter is not set. – fischi Mar 4 '14 at 11:13
  • I tried wrapping the $query->query_vars[] inside a "if ( ! isset( $_POST['test'] ) )", and adding "curl_setopt( $ch, CURLOPT_POSTFIELDS, 'test=1' );" to my cURL call, still the same - I probably did it wrong – andrux Mar 4 '14 at 14:12
  • yup, I did it wrong... not sure how but I did... it worked this time, fischi, care to get your comment posted as an answer to accept it? Thanks! – andrux Mar 4 '14 at 14:17

Your Answer

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

Browse other questions tagged or ask your own question.