WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] [closed] Tax query with multiple terms (11 posts)

  1. James
    Member
    Posted 1 year ago #

    Hi,

    WP 3.5.1
    Tax_query with an array of terms;

    By default tax_query in the WP Query (please, see below) returns posts, which include ANY of the submitted terms. Question is, whether is it possible to return only those posts, which include ALL the terms from an array?

    $args = array(
    	'post_type' => 'post',
    	'tax_query' => array(
    		array(
    			'taxonomy' => 'movie_genre',
    			'field' => 'slug',
    			'terms' => array( 'action', 'comedy' )
    		)
    	)
    );
    $query = new WP_Query( $args );

    thanks!

  2. James
    Member
    Posted 1 year ago #

    ok, got it!

  3. notkristina
    Member
    Posted 1 year ago #

    For anyone else looking for the solution to this problem, did you solve it by adding this to the inside array?

    'operator' => 'AND'

  4. James
    Member
    Posted 1 year ago #

    hi, no, it won't solve the issue, since idea of the 'AND' is the same as an array( 'action', 'comedy' ). Basically, if you have 'terms' => array( 'action') AND 'terms' => array('comedy'), it will return you all the posts containing SOME of the terms, only here you can query different taxonomies at once - at least from my experience.

    If I remember correctly, I solved it like this:

    $args = array(
    	'post_type' => 'post',
            'taxonomy_name' => 'action+comedy',
    	)
    );
    $query = new WP_Query( $args );
  5. James
    Member
    Posted 1 year ago #

    I mean 'name of your taxonomy' => 'action+comedy'

  6. grapesodabanked
    Member
    Posted 6 months ago #

    Hey, does anybody know how I could put multiple terms into the terms array with php variables? I'm trying to use this in a shortcode like so -

    from the posts query ($params gets whatever I fill in in the shortcode):

    'tax_query' => array(
                		array(
                    'taxonomy' => $params['taxonomy'],
                    'field' => 'slug',
                    'terms' => array($params['terms']),

    Then I generate a list of terms dynamically, like this:

    foreach($terms  as $term ) {
            $termslist=$termslist.'\''.$term->slug.'\'' . ', ';
            }
           $termslist = substr($termslist,0,-2);

    Which returns something like this if I echo it:

    'term1', 'term2', 'term3'

    My shortcode looks something like this:

    [shortcode taxonomy="some_taxonomy" terms="'.$termslist.'"]

    It works with a single term, but when I add more, it dies completely. Anyone know why?

  7. James
    Member
    Posted 6 months ago #

    hi grapesodabanked!

    I could help you, but you should explain what exactly do you want to achieve, since at the moment it's hard to understand your idea.

    If I understand correctly, you want to take a string from $termslist (comma separated list) and find these terms in "some_taxonomy".

    If this is what you want, all you have to do is to use:

    $terms = explode(',', $termslist);

    and use it in your query as 'terms' => $terms

    Please correct me if this is not what you want. thanks!

  8. grapesodabanked
    Member
    Posted 6 months ago #

    I'm sorry, I should have posted the entire code snippet. See, this gets me a list of terms associated with the post this snippet is called in

    $termslist='' ;
    $terms=get_the_terms( $post->ID, 'some_taxonomy');
    foreach($terms  as $term ) {
            $termslist=$termslist.'\''.$term->slug.'\'' . ', ';
            }
           $termslist = substr($termslist,0,-2);
    echo $termslist;

    Then, I want to dynamically display posts with the same terms in the "some_category" taxonomy with a shortcode like this

    echo do_shortcode('[display_posts taxonomy="some_taxonomy" terms="'.$termslist.'"]');

    Now, the php file that builds the post query for this shortcode basically does this:

    $query = array(
                        'tax_query' => array(
                		array(
                             'taxonomy' => $params['taxonomy'],
                             'field' => 'slug',
                             'terms' => array($params['terms']),
                )
            )
    			);

    So, normally, $params['terms'] should fill the terms array with the comma separated $termslist, right? And it DOES work when I have only term associated with the post. But when I add another term, BAM, it returns nothing... Driving me crazy! Am I missing something here?

  9. grapesodabanked
    Member
    Posted 6 months ago #

    ALRIGHT, in my case, I guess it had something to do with the apostrophes and commas getting lost in the translation while dynamically filling the array. It just returned a string, which included the commas, so the entire string was being read as one argument. I solved it by changing

    'terms' => array($params['terms']),

    to

    'terms' => explode(',',$params['terms']),

    This way, the shortcode will take the dynamically generated list, import it as a string and THEN explode it into an array, which I needed.

    Thanks for putting me on the right track here, James! Hope this is useful for others as well!

  10. kanabi
    Member
    Posted 5 months ago #

    Hi,

    I'm trying to achieve something similar - to display titles of posts with the same custom taxonomy terms as a current post/page. The code below can successfully display the $termslist (comma-separated terms' slugs in brackets), but it fails in this fragment:
    'terms' => explode(',',$params['terms']),

    When I change it to:
    'terms' => $termslist,
    it works on a page with only single term set.

    Any suggestions would be appreciated:)

    Full code:

    <?php
    $taxonomies = array(
      'practice',
      'sector',
    );
    $terms=get_the_terms($post->ID,$taxonomies);
    foreach($terms as $term) {
      $termslist=$termslist.'\''.$term->slug.'\'' . ', ';
    }
    $termslist = substr($termslist,0,-2);
    echo $termslist;
    $args = array(
      'post_type' => 'post',
      'posts_per_page' => 5,
      'tax_query' => array(
        array(
          'taxonomy' => 'practice','sector',
          'field' => 'slug',
          'terms' => explode(',',$params['terms']),
        )
      )
    );
    $my_query = new WP_Query($args);
    if( $my_query->have_posts() ) {
      while ($my_query->have_posts()) : $my_query->the_post(); ?>
      <h2><?php the_title(); ?></h2>
      <?php
      endwhile; }
    wp_reset_query();
      ?>
  11. esmi
    Forum Moderator
    Posted 5 months ago #

    @kanabi: If you require assistance then, as per the Forum Welcome, please post your own topic.

    This 11 month old, resolved. topic references an old version of WordPress.

Topic Closed

This topic has been closed to new replies.

About this Topic