Title: Dynamic Link from WP_Query
Last modified: August 20, 2016

---

# Dynamic Link from WP_Query

 *  [dadvan](https://wordpress.org/support/users/dadvan/)
 * (@dadvan)
 * [13 years, 9 months ago](https://wordpress.org/support/topic/dynamic-link-from-wp_query/)
 * Hello,
 * I need help with pulling in the tag name found by the query in order to create
   a link to view all the contents of the tag. Please see the code below:
 *     ```
       <?php
       global $post;
       $my_query_args = array(
           'posts_per_page' => 10, // change this to any number or '0' for all
           'post_type' => 'post',
           'tax_query' => array(
               array(
                   'taxonomy' => 'post_tag',
                   'field' => 'slug',
                   'terms' => $post->post_name // this gets the page slug
               )
           )
       );
       // a new instance of the WP_query class
       $my_query = new WP_Query( $my_query_args );
   
       if( $my_query->have_posts() ) : while( $my_query->have_posts() ) : $my_query->the_post();
       ?>
   
       <ul>
       <li><a>"><?php the_title() ?></a> <span class="authordate">-Written by <?php the_author() ?> on <?php the_time('m/j/y') ?></span></li>
       </ul>
       <?php endwhile; endif;
       echo "<h2>Displaying $my_query->post_count of $my_query->found_posts Articles</h2>";
       ?>
       <a href="/tag/<?php $my_query->post_name?>">See All Related Articles</a><?php wp_reset_postdata(); ?>
       ```
   

Viewing 1 replies (of 1 total)

 *  Thread Starter [dadvan](https://wordpress.org/support/users/dadvan/)
 * (@dadvan)
 * [13 years, 9 months ago](https://wordpress.org/support/topic/dynamic-link-from-wp_query/#post-3024961)
 * So I replaced this:
 *     ```
       <a href="/tag/<?php $my_query->post_name?>">See All Related Articles</a>
       ```
   
 * With this:
 *     ```
       <a href="/tag/<?php echo $post->post_name; ?>">See All Related Articles</a>
       ```
   
 * I also moved it after:
 *     ```
       <?php wp_reset_postdata(); ?>
       ```
   
 * Hope this helps someone!

Viewing 1 replies (of 1 total)

The topic ‘Dynamic Link from WP_Query’ is closed to new replies.

## Tags

 * [tag](https://wordpress.org/support/topic-tag/tag/)
 * [wp_query](https://wordpress.org/support/topic-tag/wp_query/)

 * In: [Hacks](https://wordpress.org/support/forum/plugins-and-hacks/hacks/)
 * 1 reply
 * 1 participant
 * Last reply from: [dadvan](https://wordpress.org/support/users/dadvan/)
 * Last activity: [13 years, 9 months ago](https://wordpress.org/support/topic/dynamic-link-from-wp_query/#post-3024961)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
