0

I am doing a WP_Query trying to filter out the posts by Metadata.

    $args = array(
      'ignore_sticky_posts'=> 1,
      'post_type' => 'post_projects',
      'post_status' => 'publish',
      'posts_per_page' => $nr_posts,
      'meta_query' => array(
        'relation' => 'OR',
        array(
          'key' => 'wpl_goal_amount',
          'value' => 100,
          'compare' => '!=',
        ),
        array(
          'key' => 'wpl_location',
          'value' => 'Europe',
          'compare' => '=',
        ),
      ),
    );

I am testing this out with two posts. One of them has "Europe" in it's array of locations, but whenever I run this, it doesn't come up with anything.

QUESTION UPDATE:

I am using Option Tree to save the Meta-data, and as mentioned in a previous answer, it stores serialized the post meta-data (by Wordpress' default), and in combination with that and another post, I found that there are two ways to query the serialized data. The first is more convenient, but more of a jerry-rig, and the second is more proper, but difficult:

  1. The easier:

        array(
          'key' => 'wpl_location',
          'value' => '"' . $my_value . '"',
          'compare' => 'LIKE'
        ),
    
  2. Compared to the better but more difficult:

       array(
          'key' => 'wpl_location',
          'value' => serialize(array(3 => 'Europe')),
          'compare' => 'LIKE'
        ),
    

I believe the second method would require me to have a (near) identical serialized array as the database, which would not work in the long-term for the system that I am creating, because it will need to match one out of many items in the array.

My updated question:

Should I continue down this path and use the first method, or do I need to dump Option Tree and figure out a way to add the meta-data to the posts in unserialized form so that it can be queried?

0

You might be missing the 'meta_key' arg here:

$args = array(
      'ignore_sticky_posts'=> 1,
      'meta_key'   => 'wpl_location', // here
      'post_type' => 'post_projects',
      'meta_query' => array(
        array(
          'key' => 'wpl_location',
          'value' => 'Europe',
          'compare' => 'IN',
        ),
      ),
    );

source: https://codex.wordpress.org/Class_Reference/WP_Query (about three quarters down the page).

  • I believe 'key' is used instead of 'meta_key', when 'meta_query' is used with an Array of Array(s) are used. – lawdawg Jul 9 '15 at 15:55
  • agree with you: have you given the 'compare' => '=', a shot? I believe the 'IN' is for multiple meta_query args. – Ray Flores Jul 9 '15 at 15:59
  • Yes, my full function actually has a compare with multiple arrays here, but I put this function up as a simplified one. But anyways, I tried both the ways you mentioned. – lawdawg Jul 9 '15 at 16:04
  • then you may have to go all the way to just the meta_query itself: codex.wordpress.org/Class_Reference/WP_Meta_Query then add that into your WP_Query( $args ) – Ray Flores Jul 9 '15 at 16:09
  • I changed the $args to the full one that I currently have. It'll get bigger than that, but it shows why the arrays are needed for 'meta_query' – lawdawg Jul 9 '15 at 16:13
0

If you're saving an array into Metadata it becomes Serialized. This makes it pretty difficult to query on, maybe try using wildcard:

$args = array(
    'ignore_sticky_posts'=> 1,
    'post_type'  => 'post_projects',
    'meta_query' => array(
        array(
            'key'     => 'wpl_location',
            'value'   => '%Europe%',
            'compare' => 'LIKE',
        ),
    ),
);

This will search the serialized data for anything that looks like "Europe".

  • Negative, but it is supposed to still be able to be done with serialized data, because the get_post_meta() function will actually give an unserialized array back if $single is set to 'true'. – lawdawg Jul 9 '15 at 15:58
  • @lawdawg get_post_meta() does but WP_Query does not. WP_Query does not use get_post_meta() in any way, just plain ole' SQL. You may need to break that out of an array and save it as a normal string. – Howdy_McGee Jul 9 '15 at 16:00
  • Oh yeah, I wasn't thinking about that. But anyways, I tried using that method and still didn't work. – lawdawg Jul 9 '15 at 16:06
  • I'm thinking this might answer our question: wordpress.stackexchange.com/questions/16709/… – lawdawg Jul 9 '15 at 16:26
  • @lawdawg Yeah, if you need to do any kind of post_meta filtering it's usually advised that you save that data separated as a string instead of serialized with your other data. – Howdy_McGee Jul 9 '15 at 16:40

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.