Cannot make a meta_query on custom field work
-
Up to now, I’ve only used tax_query in my gallery short codes, and it works fine even with 7 different taxonomies and a text search (see the sidebar multi-criteria search on the website).
Now, I would like to add a gallery displaying the pictures of one or a couple following shooting days. Each picture having a custom field name
pic_ref
that is formatted likeYYMMDD-nnnn
, with YYMMDD being the shooting date, and nnnn the original picture number, I need to look for pictures with apic_ref
greater or equal than date1-0000 and less or equal than date2-9999.I tried to adapt a working shortcode and use a meta_query instead of a tax_query, but the shortcode keeps returning the content of the whole gallery instead of the selected dates. I did a
var_export
to check the shortcode syntax and couldn’t find any error.Here is the code:
$args = array( 'public' => true, '_builtin' => false ); $my_meta_query = 'array('; if (strlen($wp_query->query_vars['du'])) { echo '<li>du ' . display_creation_date($wp_query->query_vars['du']) . '</li>'; $first_pic = $wp_query->query_vars['du'] . '-0000'; $my_meta_query .= "array('meta_key' => 'pic_ref', 'meta_value' => '$first_pic', 'meta_compare' => '>='),"; if (strlen($wp_query->query_vars['au'])) { echo '<li>au ' . display_creation_date($wp_query->query_vars['au']) . '</li>'; $last_pic = $wp_query->query_vars['au'] . '-9999'; } else { $last_pic = $wp_query->query_vars['du'] . '-9999'; } $my_meta_query .= "array('meta_key' => 'pic_ref', 'meta_value' => '$last_pic', 'meta_compare' => '<='),"; $my_meta_query .= "'relation' => 'AND'"; } $my_meta_query .= ')'; $content = do_shortcode(sprintf('[mla_gallery meta_query="%s" my_filter="term gallery" paged=current meta_key="pic_ref" orderby="meta_value" order="ASC"]', $my_meta_query)); if ( function_exists('slb_activate') ) { $content = slb_activate($content); } echo $content;
Thanks for helping.
https://www.ads-software.com/plugins/media-library-assistant/
- The topic ‘Cannot make a meta_query on custom field work’ is closed to new replies.