0

I have run into a problem with arrays.

I need to overwrite search query and exclude posts. To do that, all I need is to exclude posts that have meta value called "hide_this_page" equal to "Yes".

So I have tried with meta query:

$wp_query = new WP_Query( 

array( 

    'post_type' => array('post','page', 'landing'),
    's' => $_GET["s"], 
    'meta_query' => array(
        array(
           'key' => 'hide_this_page',
           'value' => 'Yes',
           'compare' => '!=',
        )
   )

) );

But that returns no results.

Than I have tried other option and that was to use the 'post__not_in' argument like that:

$query = new WP_Query( 'post__not_in' => array( 2, 5, 12, 14, 20 ) ) );

But I have failed to create the array( 2, 5, 12, 14, 20 ) in PHP, I cannot get rid of the key value from the array. Can anybody please help with one or the other way?

Thank you very much!

  • Does the meta key "hide_this_page" exist with value "no" on posts that you don't want to hide, or does it not exist at all on those posts? – Industrial Themes Mar 20 '17 at 15:08
  • 1) You have an extra ) in your last code block - "$query = new WP_Query(....)"; 2) If you want just the value of the array and not the keys, use the array_values() function. – Nathan Johnson Mar 20 '17 at 16:43
  • Thanks Nathan, you are right, that was part of the problem. But the array_values() does not help in this case. I need the array to look like this: array( 2, 5, 12, 14, 20 ) while array_values() prints this Array ( [0] => 1867 [1] => 1439 ): Any Ideas? – Josif Mar 21 '17 at 8:20
  • OK, now I have some results, but it still does not work properly: I have changed the post meta from "Yes" to 1 and this is how the query looks like: $wp_query = new WP_Query( array( 'post_type' => 'any', 's' => $_GET["s"], 'meta_query' => array( array( 'key' => 'hide_this_page', 'value' => "1" , 'compare' => '!=', 'type' => 'NUMERIC' ) ) ) ); However the problem is that if there is no key value (hide_this_page) the post gets excluded. How to get arround of that? – Josif Mar 21 '17 at 8:23
0

post__not_in function is for exclude categoryes. Try with:

$query = new WP_Query( 'post__not_in' => array( 2, 5, 12, 14, 20 ) );

maybe your problem is in syntax

  • Yes you are right, as Nathan pointed out, I had a obsolete bracket there... – Josif Mar 21 '17 at 8:23

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.