WordPress.org

Ready to get started?Download WordPress

Forums

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

  1. jomccartin
    Member
    Posted 2 years 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
    Forum Moderator
    Posted 2 years 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 2 years 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
    Theme Review Admin
    Posted 2 years 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 2 years 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 2 years 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

  7. linzu
    Member
    Posted 1 year ago #

    If someone still struggles with this one, here's one way:

    First query all posts that meet your other criteria. After that, in the following while loop check if the value is an array or a string (I had a nice mix after fooling around with Advanced Custom Fields). Then it's just comparing if the values match and there you go.

    Of course, if you have more than one element on your array, you need to loop through it. It's horribly arbitrary way of getting it done, but works if really need be.

    Here's my working bit. $rel_album might or might not be an array, and needs to be matched with $album_id:

    ...
    $song = new WP_Query( $song_args );
    while ( $song->have_posts() ) : $song->the_post();
    $rel_album = get_post_meta($post->ID, "related_album", true);
    if(is_array($rel_album)) :
    $rel_album = reset($rel_album);
    endif;
    if($album_id == $rel_album) :
    get_template_part( 'content', 'video' );
    endif;
    endwhile;

Topic Closed

This topic has been closed to new replies.

About this Topic