• Resolved tosca30

    (@tosca30)


    I’m trying to define a archive.php file in order to display a gallery of all the pictures linked to a specific term, but I’ve not succeeded in writing the tax_query parameter so MLA interprets it correctly.

    These few statements give me the data I need to put in tax_query:

    $mytax_query = array(
    	taxonomy	=> $wp_query->query_vars['taxonomy'],
    	field		=> 'slug',
    	terms		=> $wp_query->query_vars['term'],
    );

    but I couldn’t get further. I tried to replicate the example from the documentation, but when put in the echo do-shortcode() statement, it brings this error message:
    ERROR: Invalid mla_gallery tax_query = 'Array'.

    Could you give an example of code to be included in a PHP file.
    Thanks.

    https://www.ads-software.com/plugins/media-library-assistant/

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

    (@dglingren)

    Thanks for your questions and for posting the source code of your tax query.

    The tax query is powerful, but the syntax is a bit tricky. There are some good examples in the WP_Query Codex documentation, and you can find an MLA-specific example in a recent support topic.

    The syntax allows you to compose a query on multiple taxonomies, so the tax_query value must be an array of taxonomy entries. In you case, the query just needs one more level of array() delimiters:

    $mytax_query = array(
    	array(
    		taxonomy	=> $wp_query->query_vars['taxonomy'],
    		field		=> 'slug',
    		terms		=> $wp_query->query_vars['term'],
    	)
    );

    That’s the most common mistake; I make it all the time.

    Your example is a simple taxonomy=term query, and you could get by with the “simple Taxonomy Query” described in the Codex. The newer tax_query form you’re using is preferred and gives you room to grow.

    You can find a couple of other examples in the mla-child theme included with the recent MLA versions. Look in the media-library-assistant/examples/mla-child directory for the functions.php file. In that file you can find mla_tag_gallery() and a more elaborate mla_paginated_term_gallery() example.

    I am marking this topic resolved, but please update it if you have any problems or more questions about the topic. Thanks for the question anf for your interest in the plugin.

    Thread Starter tosca30

    (@tosca30)

    I’ve modified my tax_query, that is now:

    array (
      0 =>
      array (
        'taxonomy' => 'lieu',
        'field' => 'slug',
        'terms' => 'nasbinals',
      ),
      1 =>
      array (
        'taxonomy' => 'cadrage',
        'field' => 'slug',
        'terms' => 'plan-large',
      ),
      2 =>
      array (
        'taxonomy' => 'traitement',
        'field' => 'slug',
        'terms' => 'couleur',
      ),
      3 =>
      array (
        'taxonomy' => 'tag_perso',
        'field' => 'slug',
        'terms' => 'golden-hour',
      ),
      4 =>
      array (
        'taxonomy' => 'attachment_category',
        'field' => 'slug',
        'terms' => 'paysages',
      ),
      5 =>
      array (
        'taxonomy' => 'attachment_tag',
        'field' => 'slug',
        'terms' => 'aubrac',
      )

    and am trying to display the gallery with this:
    echo do_shortcode(sprintf('[mla_gallery tax_query="%s" paged=current]', $my_tax_query));
    but I still get the message:

    ERROR: Invalid mla_gallery tax_query = ‘Array’

    I didn’t find any example with a tax_query parameter, so I don’t know what it still wrong.

    Thread Starter tosca30

    (@tosca30)

    I just saw that I’d forgotten to put the relation parameter.
    New tax_query:

    array (
      'relation' => 'AND',
      0 =>
      array (
        'taxonomy' => 'lieu',
        'field' => 'slug',
        'terms' => 'nasbinals',
      ),
      1 =>
      array (
        'taxonomy' => 'cadrage',
        'field' => 'slug',
        'terms' => 'plan-large',
      ),
      2 =>
      array (
        'taxonomy' => 'traitement',
        'field' => 'slug',
        'terms' => 'couleur',
      ),
      3 =>
      array (
        'taxonomy' => 'tag_perso',
        'field' => 'slug',
        'terms' => 'golden-hour',
      ),
      4 =>
      array (
        'taxonomy' => 'attachment_category',
        'field' => 'slug',
        'terms' => 'paysages',
      ),
      5 =>
      array (
        'taxonomy' => 'attachment_tag',
        'field' => 'slug',
        'terms' => 'aubrac',
      ),
    )

    but still the same error message.

    Thread Starter tosca30

    (@tosca30)

    It seems that the problem comes from using a variable as a parameter in the do_shortcode statement.
    I don’t know how to pass this parameter: tax_query=$my_tax_query; the use of sprintf only returns Array, and that causes the error.

    Plugin Author David Lingren

    (@dglingren)

    Thanks for these updates, especially the last one, which reveals the clue to your issue. As you’ve discovered, the sprint/do_shortcode requires a string value for the tax_query parameter. As painful as it looks, you have to format your specification as one long string:

    tax_query="array ( 'relation' => 'AND', 0 => array ( 'taxonomy' => 'lieu', 'field' => 'slug', 'terms' => 'nasbinals', ), 1 => array ( 'taxonomy' => 'cadrage', 'field' => 'slug', 'terms' => 'plan-large', ), 2 => array ( 'taxonomy' => 'traitement', 'field' => 'slug', 'terms' => 'couleur',), 3 => array ('taxonomy' => tag_perso',  'field' => 'slug', 'terms' => 'golden-hour', ), 4 => array ( 'taxonomy' => 'attachment_category', 'field' => 'slug', 'terms' => 'paysages', ), 5 => array ( 'taxonomy' => 'attachment_tag', 'field' => 'slug', 'terms' => 'aubrac', ),)"

    That should get through the sprint and into the do_shortcode. You can build up the string with sprintf and concatenation statements, but it needs to be a pure string with no line breaks when you’re done.

    Thread Starter tosca30

    (@tosca30)

    Thanks for your answer, but I don’t know how to fix my problem: the taxonomy/terms parameters will be selected from a searchform by the user, and I have no way to know in advance which will be chosen; that’s why I can’t use constants and need variable(s) in the tax_query instead.

    Here is the searchform I intend to use.
    How can I display an archive gallery, based on this multi-criteria query?

    Thread Starter tosca30

    (@tosca30)

    Eventually, I reached a solution by building a $my_tax_query string, that I use in the do_shortcode as follows:
    echo do_shortcode(sprintf('[mla_gallery tax_query="%s" paged=current]', $my_tax_query));

    There are still some refinements to be done, but I think I’m getting to right end of the stick.

    Thanks a lot for helping.

    Plugin Author David Lingren

    (@dglingren)

    Thanks for the update and the good news. It sounds like you have picked up on my hint (“You can build up the string with sprintf and concatenation statements“) and found a solution that will work.

    If you have any other problems or further questions, let me know. When you get a final solution you are satisfied with, consider posting it here so other users can learn from it.

    Thanks for your help and your continued interest in the plugin.

    Thread Starter tosca30

    (@tosca30)

    Here is what I’ve written in order to build the tax_query string:

    $args = array(
    'public'   => true,
    '_builtin' => false
    );
    $my_taxonomies = get_taxonomies($args);
    $my_tax_query = 'array(';
    $i = 0;
    foreach ($my_taxonomies as $my_taxonomy) {
    	if (!strlen($wp_query->query_vars[$my_taxonomy])) {
    		continue;
    	}
    	$i++;
    	$my_terms = $wp_query->query_vars[$my_taxonomy];
    	$my_tax_query .= "array('taxonomy' => '" . $my_taxonomy . "', 'field' => 'slug', 'terms' => '" . $my_terms . "'),";
    }
    if ($i > 1) {
    	$my_tax_query .= "'relation' => 'AND'";
    }
    $my_tax_query .= ')';
    echo do_shortcode(sprintf('[mla_gallery tax_query="%s" my_filter="term gallery" paged=current]', $my_tax_query));

    There is still one thing that I couldn’t make work: I use the Simple Lightbox extension to display the pictures of a gallery. It works fine when the gallery is built from a shortcode on a specific page, but not on any archive page where the galley is displayed using code with do_shortcode, for example; the attachment article-page is displayed instead of the lightbox, though I have enabled Simple Lightbox for every type of page.

    Any idea how to solve this?

    Plugin Author David Lingren

    (@dglingren)

    Thanks for posting your code, which looks like a good solution.

    I don’t have any experience with Simple Lightbox, but based on the two example links you posted it looks like your custom Archive page is not being recognized by the plugin’s code for adding the attributes it needs to the gallery item links.

    If you are on the latest version (2.3.0) you can try the new slb_activate() template function to process the gallery output. Have a look at:

    What’s New in Simple Lightbox 2.3.0

    I believe your code would look like this:

    $content = do_shortcode(sprintf('[mla_gallery tax_query="%s" my_filter="term gallery" paged=current]', $my_tax_query));
    if ( function_exists('slb_activate') ) {
        $content = slb_activate($content);
    }
    echo $content;

    Give that a try and let me know if it gets you the lightbox effects you want.

    Thread Starter tosca30

    (@tosca30)

    Thanks for having investigating this.

    It doesn’t work better after I’ve modified the code: I checked the condition is actually true, but there is no change in the output. This SLB version seems to be still in beta, and other users have reported such issues; I have added my input to Simple Lightbox support and will let you know when/how the problem is fixed.

    Thread Starter tosca30

    (@tosca30)

    Eventually find my mistake: I use MLA shortcode several times in the page, for the gallery itself, then for the pagination links; and I had added the Simple Lightbox code to the wrong instance!

    Once it is put on the ‘images’ gallery, it works fine.

    Plugin Author David Lingren

    (@dglingren)

    That is excellent news; thanks for the update. Just to be clear, did you end up using the slb_activate function as described above?

    Thread Starter tosca30

    (@tosca30)

    Yes, I use the slb_activate function; without it, the lightbox didn’t work and the media page (= single post page) was displayed instead.

Viewing 14 replies - 1 through 14 (of 14 total)
  • The topic ‘Using tax_query parameter in do_shortcode’ is closed to new replies.