Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Query a CTP based on Custom Field, PHP issue. WordPress

  • SOLVED

<strong>Task:</strong> I need to query a custom post type (random quote) in the sidebar of a page, I want to have a custom field on that page with the value 'apples' and apply the same custom filed to the CPT post.

<strong>Issue:</strong> My code works by pulling the Meta Value and storing in a function, but this only works with single entries, as soon as I add multiple values IE: "apples, banannas," the entire query breaks.


$tag = get_post_meta($post->ID, 'cf-tag', true);
//var_export($tag);

query_posts(

array(
'post_type' => 'quotes',
'meta_key' => 'cf-tag',
'meta_values' => $tag
//'posts_per_page' => 10,
//'orderby' => 'rand',
)

);

while(have_posts())

{
the_post();
the_title('');
the_content('');
}

wp_reset_query();


<em>Any suggestions you can provide would be much appreciated!</em>

Answers (4)

2012-01-23

Luis Abarca answers:

You need something like this, using [[LINK href="http://codex.wordpress.org/Class_Reference/WP_Query#Custom_Field_Parameters"]]meta_query[[/LINK]]

$tag = array('bananas', 'apples');

$args = array(
'post_type' => 'quotes',
'meta_query' => array(
array(
'key' => 'cf-tag',
'value' => $tag,
'compare' => 'LIKE'
)
),
'orderby' => 'rand'
);
$query = new WP_Query( $args );


while( $query->have_posts() )
{
$query->the_post();
the_title('');
the_content('');
}

wp_reset_query();


Luis Abarca comments:

I put the $tag as un array as example, i think you are getting tha $tag value from the current post tags or something like that ?


West Coast Design Co. comments:

Hi Luis,

I won't know the values of these custom field, these values will interact similar to tags.

<strong>I need:</strong>
$tag = get_post_meta($post->ID, 'cf-tag', true);

<strong>To output:</strong>
"apples, bananas,"

<strong>Inside:</strong>
'meta_values' => $tag

<em>Make sense?</em>


Luis Abarca comments:

Yep, as Arnav said, you need to change to false the last argument of get_post_meta to get an array instead of a single value.


$tag = get_post_meta($post->ID, 'cf-tag', false);


Updated:

<strong>$tag = get_post_meta($post->ID, 'cf-tag', false);</strong>
$args = array(
'post_type' => 'quotes',
'meta_query' => array(
array(
'key' => 'cf-tag',
'value' => $tag,
)
),
'orderby' => 'rand'
);

$query = new WP_Query( $args );

while( $query->have_posts() )
{
$query->the_post();
the_title();
the_content();
}

wp_reset_query();


West Coast Design Co. comments:

Luis,

False outputs an Array, but doesn't seem to work with

'value' => $tag,

<em>Any suggestions, thanks for the assistance!</em>


Luis Abarca comments:

OK, so you are storing a list of values separated by comma: "apples, bananas,"

Using the code from Arnav.


$tag = get_post_meta($post->ID, 'cf-tag', true);

$tag = explode(",",$tag);

$args = array(
'post_type' => 'quotes',
'meta_query' => array(
array(
'key' => 'cf-tag',
<strong>'value' => array_values($tag),</strong>
)
),
'orderby' => 'rand'
);


West Coast Design Co. comments:

Correct, nothing seems to be working lol.

2012-01-23

Arnav Joy answers:

replace
$tag = get_post_meta($post->ID, 'cf-tag', true);
with
$tag = get_post_meta($post->ID, 'cf-tag', false);


Arnav Joy comments:

try this
$tag = get_post_meta($post->ID, 'cf-tag', true);

$tag = explode(",",$tag);


Arnav Joy comments:

try my first answer if you are adding multiple meta box with the same
and try my second answer if you are using single meta box with values seperated with ,


West Coast Design Co. comments:

Arnav,


Thanks! However, your second method outputs:

array ( 0 => 'demo', )

with doesn't seem to work with

'value' => $tag,

<strong>Current Code:</strong>


$tag = get_post_meta($post->ID, 'wcgi-tag', true);
$tag = explode(",",$tag);
//var_export($tag);


$args = array(

'post_type' => 'customerspotlight',
'meta_query' => array(

array(

'key' => 'wcgi-tag',
'value' => $tag,

)

),

'orderby' => 'rand'

);


$query = new WP_Query( $args );



while( $query->have_posts() )

{

$query->the_post();

the_title();

the_content();

}

wp_reset_query();


Arnav Joy comments:

did you placed a , at last in your custom filed value?


West Coast Design Co. comments:

Sadly yes :(

lol.


Arnav Joy comments:

try this

$tag = get_post_meta($post->ID, 'cf-tag', false);
$tag = explode(",",substr($tag,0,strlen($tag)-1)));


Arnav Joy comments:

try this as their was an extra) in last code...

$tag = get_post_meta($post->ID, 'cf-tag', false);
$tag = explode(",",substr($tag,0,strlen($tag)-1));


Arnav Joy comments:

if you are placing , at last then use

$tag = explode(",",substr($tag,0,strlen($tag)-1));

as this will remove last , from the string

otherwise use

$tag = explode(",",$tag);

2012-01-23

Julio Potier answers:

<em>Luis and Arnav spent a lot of time, this question is too low rated. I suggested $15.
Thank you to think about it, and them.</em>


West Coast Design Co. comments:

This was actually through my work's CC, hence why I didn't up the prize. I will figure it out.

2012-01-23

Mike Van Winkle answers:

Julio, I think the issue is that you need to set the "COMPARE" value to 'IN'. If you take Luis' code and just change the 'compare' => 'LIKE' ... to 'compare'=>'IN' ... you should be good.


$tag = array('bananas', 'apples');
$args = array(
'post_type' => 'quotes',
'meta_query' => array(
array(
'key' => 'cf-tag',
'value' => $tag,
'compare' => 'IN'
)
),
'orderby' => 'rand'
);
$query = new WP_Query( $args );
while( $query->have_posts() )
{
$query->the_post();
the_title('');
the_content('');
}
wp_reset_query();