Forums

Custom query filtered by meta_value that is an array? (6 posts)

  1. jomccartin
    Member
    Posted 4 months ago #

    Hi Everyone-
    I just walked into the project close to its finish. They're using custom fields where they should likely be using custom taxonomies, and I'm trying to work around that. I can't really change their data structure at this point, so responses like "Use Custom Taxonomies," while correct, are not apt.

    I'm trying to run a custom query that filters by meta_value. The value is an array though. Were it not an array, the following code would work.

    $args = array(
    		  'numberposts' => -1,
    		  'post_type' => 'post',
    		  'meta_query' => array (
    		    array (
    			  'key' => 'my_key',
    			  'value' => 'target_value',
    		    )
    		  ) );		
    
    		$new_query = new WP_Query( $args );

    Since the key 'my_key' always has an array as its value, it will never match the queried 'target_value'. How do I find the 'target_value' in the array? This is an array of strings by the way.

    Any help would be really appreciated!!
    Thanks. -John

  2. esmi
    Theme Diva & Forum Moderator
    Posted 4 months ago #

    I have absolutely no idea if this will work but what about trying:

    meta_query' => array (
    		    array (
    			  'key' => 'my_key',
    			  'value' => in_array( 'target_value', 'my_key'),
    		    )
  3. jomccartin
    Member
    Posted 4 months ago #

    Hey, thanks for the input! That didn't do it, but it did give me the push to figure out a partial solution. It's not really what I wanted to do, and if this group ends up having a lot of posts, it's going to get very slow & clunky. Instead of filtering by value, I filter only by key, and then display only posts with the correct value using an IF statement.

    <?php
    $args = array (
    ...
    'meta _query' => array ( array (
                                      'key' => 'my_key'
                                   )  )
    )
    $new_query = new WP_Query( $args );
    
    ...//start loop...
    
    $values = get_post_meta($post->ID, 'my_key', false);
    $foreach ($values as $val_sub) :
          if ( in_array('target_value', 'my_key') ) : ?>
               <div class="post">...</div>
          <?php endif;
    endforeach; ?>

    So yea, it's not ideal in any way and doesn't really solve the problem. But it's going to display the right posts the very least.

    This seems to be a lingering issue. There's been a few posts on dealing with arrays as meta_values, and the Codex itself requests a solution in the orderby meta_value section. I'm not going to mark this resolved since the central issue isn't closed, but I did want to share my workaround.

  4. Chip Bennett
    Member
    Posted 4 months ago #

    The WP_Query() custom field (i.e. meta) query can handle arrays for field values. You just need to add the compare key to your array:

    value (string|array) - Custom field value (Note: Array support is limited to a compare value of 'IN', 'NOT IN', 'BETWEEN', or 'NOT BETWEEN')

    For example, this should work:

    $args = array(
    		  'numberposts' => -1,
    		  'post_type' => 'post',
    		  'meta_query' => array (
    		    array (
    			  'key' => 'my_key',
    			  'value' => 'target_value',
                              'compare' => 'IN'
    		    )
    		  ) );		
    
    		$new_query = new WP_Query( $args );
  5. jomccartin
    Member
    Posted 4 months ago #

    Hi Chip-

    I've tried this and it hasn't worked, but I think I know why now. I've discovered the value of 'my_key' is actually a multidimensional array for some reason. (If you look in the second code block I posted, I run a foreach and then an in_array to drill down into these arrays.) The custom fields themselves were produced by the Advanced Custom Fields plugin. Perhaps this has something to do with it.

    After some research, the author of that plugin has actually concluded what I wanted to do is impossible: http://support.plugins.elliotcondon.com/discussion/499/how-to-query-through-multiple-select-meta_values/p1

    I'm not entirely sure it's impossible, but I don't know quite enough to offer a full-on solution.

  6. blu3s0da
    Member
    Posted 1 month ago #

    im looking for the answer to this as well, were you able to find a solution?

    would it be possible to edit and use this solution to our problem? http://stackoverflow.com/questions/1354259/how-to-write-query-based-on-array-values

    or

    http://core.trac.wordpress.org/ticket/14645

Reply

You must log in to post.

About this Topic