WP_Query with meta_value impossible on cloned data fields (array) ?
- This topic has 3 replies, 2 voices, and was last updated 10 years, 1 month ago by
Anh Tran.
-
AuthorPosts
-
March 26, 2015 at 12:02 AM #696
PascalGenest
ParticipantHi, Is there a way to store metadata value from a cloned field, in multiples entries into the DB, instead of only one entry with values into an array ? I would like the same effect as if I would have used 'add_metadata( $meta_type, $object_id, $meta_key, $meta_value, $unique )' with the '$unique' parameter set to FALSE.
I would need this because when we build a WP_Query that search for a value on a cloned field, it can find the value because the values are stored as an array serialized instead of multiple entries in the post_meta table in the MySql Database. And I can't use the 'IN' compare argument....
$args = array(
'post_type' => 'MyPosType',
'meta_key' => 'color',
'meta_value' => 'blue');$query = new WP_Query( $args );
The only way that I have is to search only on 'meta_key' and then after use 'in_array' function trough a 'foreach' loop... but this is not really a efficient solution...
March 26, 2015 at 11:10 PM #698Anh Tran
KeymasterHi,
This is an good question about how the plugin store post meta in the database. The short answer for your question is "no" and here is why:
When the "clone" feature wasn't born, the plugin stores all meta data in the data base in a WordPress-compatible way, e.g. if field has single value, it will be saved in single row of
postmeta
table; if field has multiple values (set by'multiple' => true
), each value will be saved in single row of that table. This way you can useadd_post_meta
orupdate_post_meta
field post's custom fields to retrieve meta value.But when the "clone" feature is born, it's confusing to store each cloned value in a single row of
postmeta
table. Because we don't know these are values of clones, or values of the field withmultiple = true
. In order to make it work for all field types and for consistency, I decided to store cloned values in an array and store that array in single row in thepostmeta
table.So, we can't use
WP_Query
to query based on value of a cloned field directly. I think your solution withforeach
loop is a good solution for now.March 26, 2015 at 11:52 PM #700PascalGenest
ParticipantOK, I see.
I think it would be interesting to add a option to some fields params, that would use multiple rows for clones. Example :array(
'name'=> ' the name',
'id'=> "thefield",
'desc'=> "some description",
'type'=> 'text',
'clone_advanced' => true,
)People that would use these parameter would certainly have the knowledge to use the appropriate function/query to retrieve the data, and would be aware that if they add custom fields with the same ID, the data would be grouped in the same field/clone area. I'm not sure but I think you have your own functions to retrieve the data. I suggest that you could write an additional function to get data form 'advanced clone fields'.
this would give a lot of power and flexibility to your great plugin. 🙂
),
March 27, 2015 at 9:26 AM #702Anh Tran
KeymasterThanks for your idea. It's a good solution for this purpose. I will think about it, maybe I will add hooks which allows us to do that. I will add more details and let's see how it goes.
-
AuthorPosts
- The topic ‘WP_Query with meta_value impossible on cloned data fields (array) ?’ is closed to new replies.