• Resolved tosca30

    (@tosca30)


    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 like YYMMDD-nnnn, with YYMMDD being the shooting date, and nnnn the original picture number, I need to look for pictures with a pic_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/

Viewing 2 replies - 1 through 2 (of 2 total)
  • Plugin Author David Lingren

    (@dglingren)

    Thanks for your question and for taking the time to include the details and especially the source code listing, which are very helpful. Thanks as well for the link to your site; the multi-criteria “Recherche dans la photothèque” sidebar element is inspiring!

    I set up a custom field containing the post_date in the default format, e.g., “2014-07-15 20:22:15”. I know your format is different, but the idea is similar.

    I created the following shortcode, which displays all images in a range of dates:

    [mla_gallery meta_query="array( array( 'key' => 'pic_ref', 'value' => array( '2014-06-24', '2014-12-31' ), 'compare' => 'BETWEEN' ) )"]

    Again, your code is somewhat different but following the same logic.

    Notice that my query parameters include ‘key’ and ‘value’; your parameters are ‘meta_key’ and ‘meta_value’, which will be ignored. That is why your query returns all of the images in your Media Library.

    Also, notice that my query uses just one taxonomy term and uses the ‘compare => ‘BETWEEN’ parameter to get the range, with a two-element array in the ‘value’ parameter. You might find this simpler and more convenient than two separate taxonomy terms.

    I hope you can adapt my example to the specifics of your application. If you need more specific guidance, let me know.

    I am marking this topic resolved, but please update it if you have more questions or problems with custom field queries. Thanks for an interesting question and for your continued interest in the plugin.

    Thread Starter tosca30

    (@tosca30)

    Great! Thanks a lot for having spend time and energy to find out what was wrong.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Cannot make a meta_query on custom field work’ is closed to new replies.