Problem: WP_Query() with custom field value as an array ...

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Problem: WP_Query() with custom field value as an array ...

wpmidia web
hello people!

How could I do my query (using WP_Query() class), if my custom field value
is stored as an array?

for example:
a:2:{i:0;s:3:"183";i:1;s:3:"182";}

$args = array( 'post_type' => 'MY_POST_TYPE', 'posts_per_page' => 10,
'order' => 'ASC', 'meta_key' => 'MY_KEY', 'meta_value' =>
'MY_META_VALUE_ARRAY' );
$loop = new WP_Query($args);

I need to check if a ID is stored in this array, to proceed my query ....

Somebody help me?


thanks!

--
*Míriam de Paula *
*(a.k.a. Drika)*
*Twitter:* @miriamdepaula

wp*midia*
*desenvolvimento web*
http://wpmidia.com.br
twitter: @wpmidia

*WordPress Brasil no Facebook: *
http://www.facebook.com/groups/wordpress.brasil

*OpenCart Brasil no Facebook:*
http://www.facebook.com/groups/opencart.brasil/
_______________________________________________
wp-hackers mailing list
[hidden email]
http://lists.automattic.com/mailman/listinfo/wp-hackers
Reply | Threaded
Open this post in threaded view
|

Re: Problem: WP_Query() with custom field value as an array ...

Otto-19
Most obvious answer: Don't store the custom field value as a
serialized array. You can store more than one value with a meta key,
making searching for it much easier.

For example, instead of storing the array with 183 and 182 in it,
store it as two separate meta entries with the same meta key.

add_post_meta($post_id, 'keyname', 183);
add_post_meta($post_id, 'keyname', 182);

get_post_meta($post_id, 'keyname'); // returns array(183,182);

-Otto



On Mon, Nov 14, 2011 at 11:17 AM, wpmidia lab <[hidden email]> wrote:

> hello people!
>
> How could I do my query (using WP_Query() class), if my custom field value
> is stored as an array?
>
> for example:
> a:2:{i:0;s:3:"183";i:1;s:3:"182";}
>
> $args = array( 'post_type' => 'MY_POST_TYPE', 'posts_per_page' => 10,
> 'order' => 'ASC', 'meta_key' => 'MY_KEY', 'meta_value' =>
> 'MY_META_VALUE_ARRAY' );
> $loop = new WP_Query($args);
>
> I need to check if a ID is stored in this array, to proceed my query ....
>
> Somebody help me?
>
>
> thanks!
>
> --
> *Míriam de Paula *
> *(a.k.a. Drika)*
> *Twitter:* @miriamdepaula
>
> wp*midia*
> *desenvolvimento web*
> http://wpmidia.com.br
> twitter: @wpmidia
>
> *WordPress Brasil no Facebook: *
> http://www.facebook.com/groups/wordpress.brasil
>
> *OpenCart Brasil no Facebook:*
> http://www.facebook.com/groups/opencart.brasil/
> _______________________________________________
> wp-hackers mailing list
> [hidden email]
> http://lists.automattic.com/mailman/listinfo/wp-hackers
>
_______________________________________________
wp-hackers mailing list
[hidden email]
http://lists.automattic.com/mailman/listinfo/wp-hackers