WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Custom loop using a meta_value that is an array (4 posts)

  1. Sam Scholfield
    Member
    Posted 2 years ago #

    Hi All,

    I am currently trying to output one random entry from a custom post type. The post type has a custom field called 'person_type', the values in this field are an array, e.g. array(0=>'advisor', 1=>'contributor')

    I am trying to set up a custom loop that pulls advisors only. The code I am using is:

    $args = array(
               'post_type' => $type,
               'orderby' => 'rand',
               'meta_key' => 'person_type',
               'meta_value' => 'advisor'
               );
        }
        $loop = new WP_Query($args);

    This doesn't return anything.

    I have tried changing the meta_value to:
    'meta_value' => array('advisor'),

    however this throws up an error saying:

    Warning: trim() expects parameter 1 to be string, array given in /wp-includes/meta.php on line 526

    In other words it expects a string, any help would be appreciated!

    Thanks,
    Scoe

  2. Sam Scholfield
    Member
    Posted 2 years ago #

    I changed the way it worked and put each of the array items into their own custom field.

    The $args are now:

    $args = array(
               'post_type' => $type,
               'orderby' => 'rand',
               'meta_key' => 'person_advisor',
               'meta_value' => true
               );
        }
        $loop = new WP_Query($args);

    Thanks anyway

  3. jomccartin
    Member
    Posted 2 years ago #

    Hey, I'm having the same problem, but I just walked into the project close to its finish. I can't really change their data structure at this point. Here's the code:

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

    The key 'my_key' always has an array as its value, so of course 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

  4. esmi
    Forum Moderator
    Posted 2 years ago #

    Please post a new topic.

Topic Closed

This topic has been closed to new replies.

About this Topic