WordPress.org

Ready to get started?Download WordPress

Forums

How to pass a variable into my tax_query terms parameter? (7 posts)

  1. ldwriters
    Member
    Posted 1 year ago #

    I need the terms in my tax_query to be the ones from the current post. In order to do this I am using a variable. The value of the variable is given by using the get_terms() function.

    The problem is that it is not returning anything. It does however return the correct posts when I manually put the terms in the array.

    How do I grab the posts terms automatically and place them in the terms parameter?

    Here is the code that I am using.

    <?php
    //for use in the loop, list 5 post titles related to first tag on current post
    $tags1 = get_terms($post->ID, 'b');
    if ($tags1) {
    echo '<div id="productsideheaders"><h2>PRODUCTS</h2></div>';
    $args = array(
    	'tax_query' => array(
    		array(
    			'taxonomy' => 'b',
    			'terms' => array('$tags1'),
    			'field' => 'slug',
    
    		)
    	)
    );
    $query = new WP_Query( $args );
    $my_query = new WP_Query($args);
    if( $my_query->have_posts() ) {
    while ($my_query->have_posts()) : $my_query->the_post(); ?>
    <div style="width: 270px;float: left;">
    <?php if ( has_post_thumbnail()) :  ?>
    <a>" title="<?php the_title_attribute(); ?>" >
    <?php the_post_thumbnail( array(40,40) ); ?>
    </a> <span style="font-size: 10pt;"><a>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title('' ); ?></a></span></div>
    <?php endif; ?>
     		<?php
    endwhile;
    }
    }
    ?>

    [Moderator Note: Please post code or markup snippets between backticks or use the code button. As it stands, your code has now been permanently damaged/corrupted by the forum's parser.]

  2. ldwriters
    Member
    Posted 1 year ago #

    I need the terms in my tax_query to be the ones from the current post. In order to do this I am using a variable. The value of the variable is given by using the get_terms() function.

    The problem is that it is not returning anything. It does however return the correct posts when I manually put the terms in the array.

    How do I grab the posts terms automatically and place them in the terms parameter?

    Here is the code that I am using.

    <?php
    //for use in the loop, list 5 post titles related to first tag on current post
    $tags1 = get_terms($post->ID, 'b');
    if ($tags1) {
    echo '<div id="productsideheaders"><h2>PRODUCTS</h2></div>';
    $args = array(
    	'tax_query' => array(
    		array(
    			'taxonomy' => 'b',
    			'terms' => array('$tags1'),
    			'field' => 'slug',
    
    		)
    	)
    );
    $query = new WP_Query( $args );
    $my_query = new WP_Query($args);
    if( $my_query->have_posts() ) {
    while ($my_query->have_posts()) : $my_query->the_post(); ?>
    <div style="width: 270px;float: left;">
    <?php if ( has_post_thumbnail()) :  ?>
    <a>" title="<?php the_title_attribute(); ?>" >
    <?php the_post_thumbnail( array(40,40) ); ?>
    </a> <span style="font-size: 10pt;"><a>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title('' ); ?></a></span></div>
    <?php endif; ?>
     		<?php
    endwhile;
    }
    }
    ?>
  3. wpjunior
    Member
    Posted 1 year ago #

    I am facing the same issue trying to pass the value of the variable (using get_the_terms()) to dynamically set the value of the 'terms' argument of the tax_query parameter. Have you come across any solution or workaround for this?

  4. ldwriters
    Member
    Posted 1 year ago #

    This is what I ended up doing and it works.

    `'tax_query' => array(
    array(
    'taxonomy' => 'b',
    'field' => 'slug',
    'terms' => wp_get_post_terms($post->ID, 'b', array("fields" => "names"))

  5. wpjunior
    Member
    Posted 1 year ago #

    It worked for me too! Thanks a lot Idwriters!

  6. eschmidt99
    Member
    Posted 1 year ago #

    For those of you, like me, who needed to actually pass a variable into 'terms', here is the answer, graciously provided to me in another forum post from vtxyzzy.

    Set $variable so it will echo out something like:

    yellow,black,blue

    Then, within your tax_query array, have:

    ...
    'terms' => explode(',', $variable)
    ...

    I hope this helps somebody as it helped me.

  7. bikkarbai
    Member
    Posted 1 year ago #

    thanks eschmidt99 i have been looking fot it

Topic Closed

This topic has been closed to new replies.

About this Topic