0
<div class="row">


<?php 
$taxonomyName = "product_tag";
 if($taxonomyname = "latest-product"){
//This gets top layer terms only.  This is done by setting parent to 0.  
    $parent_terms = get_terms($taxonomyName, array('parent' => 0, 'orderby' => 'term_id', 'hide_empty' => true, 'order' => 'ASC'));

    foreach ($parent_terms as $pterm) { ?>

            <div class="col-xs-3 col-md-3 padding_fix">



                <a href="<?php echo get_term_link($pterm->name, $taxonomyName); ?>">

                    <?php 
                    $thumbnail_id = get_woocommerce_term_meta($pterm->term_id, 'thumbnail_id', true);
                            // get the image URL for parent category
                    $image = wp_get_attachment_url($thumbnail_id);

                    echo '<img src="'.$image.'" alt="" width="762" height="365" />'; ?>

                    <h3 class="text-center" style="color: #fff;"><?php echo $pterm->name; ?></h3>
                </a>



            </div>

        <?php } ?>
    <?php } ?>

this is my above code where i want to display product with latest-product tag name.

I am not able to display product by latest-product tag.

0

Finally Solved my problem My previous code was only for category wise product but when using tag it should be post. for better explain someone can please explain about the whole process.

<?php 
            $args = array(
                'number'     => $number,
                'orderby'    => 'title',
                'order'      => 'ASC',
                'hide_empty' => $hide_empty,
                'include'    => $ids
            );
            $product_tags = get_terms( 'product_tag', $args );
            $count = count($product_tags);
            if ( $count > 0 ){
                foreach ( $product_tags as $product_tag ) {
                    // echo '<h4><a href="' . get_term_link( $product_tag ) . '">' . $product_tag->name . '</a></h4>';
                    $args = array(
                        'posts_per_page' => -1,
                        'tax_query' => array(
                            'relation' => 'AND',
                            array(
                                'taxonomy' => 'product_tag',
                                'field' => 'slug',
                                // 'terms' => 'white-wines'
                                'terms' => $product_tag->slug,
                                'product_tag'=> "latest-product"
                            )
                        ),
                        'post_type' => 'product',
                        'orderby' => 'title,'
                    );
                    $products = new WP_Query( $args );
                    echo "<ul>";
                    while ( $products->have_posts() ) {
                        $products->the_post();
                        ?>
                            <li>
                                <a href="<?php the_permalink(); ?>">
                                    <?php the_title(); ?>
                                    <?php echo get_the_post_thumbnail( $post_id, 'thumbnail' ); ?>
                                </a>
                            </li>
                        <?php
                    }
                    echo "</ul>";
                }
            }
         ?>
0

product_tag is non-hierarchical product category. To fetch the products you will need to run the query as below :

add_action("admin_init","display_products");
function display_products()
{
 ob_start();

 $args = array( 
            'post_type'      => 'product', 
            'posts_per_page' => -1, 
            'product_tag'    => array('latest-product')
            );
 $loop = new WP_Query( $args );
 $product_count = $loop->post_count;


 if( $product_count > 0 ){
    echo '<ul class="products">';
    while ( $loop->have_posts() ) : $loop->the_post(); 
            global $product;
            global $post;

            echo "<li>" . $loop->post->ID. " </li>";


    endwhile;

    echo '</ul>';
 }else{
       echo 'No product matching your criteria.';
 }

 $result =  ob_get_clean();
 echo $result;
 }

Note : Just to check the snippet I have added it on admin_init hook You can change it.

Tax Query can be added in arguments for supporting multiple tags.

  • Thanks for your contribution. I tried but nothing displaying. – Arman Jun 26 '19 at 3:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.