Title: Create a Shortcode for a WP_Query loop
Last modified: October 19, 2016

---

# Create a Shortcode for a WP_Query loop

 *  Resolved [Creative Andrew](https://wordpress.org/support/users/geisthanen/)
 * (@geisthanen)
 * [9 years, 7 months ago](https://wordpress.org/support/topic/create-a-shortcode-for-a-wp_query-loop/)
 * I’m a noob WordPress Developer and I just created my first Custom Template Page
   using Advanced Custom Fields and managed to loop.
 *  `<?php
    $args = array( ‘post_type’ => ‘art’, ‘orderby’ => ‘title’, ‘order’ =
   > ‘ASC’ ); $the_query = new WP_Query( $args ); ?> <?php if ( have_posts() ) :
   while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
 *  <?php get_template_part( ‘content’, ‘art’ ); ?>
 *  <?php endwhile; endif; ?>`
 * But I will like to use it not only inside a template page, but anywhere I want.
   Therefore I need to create a shortcode.
 * Example: `function foobar_func( $atts ){
    return “foo and bar”; } add_shortcode(‘
   foobar’, ‘foobar_func’ );`
 * My question would be: How can i put the loop inside my shortcode?
    -  This topic was modified 9 years, 7 months ago by [Creative Andrew](https://wordpress.org/support/users/geisthanen/).

Viewing 1 replies (of 1 total)

 *  [Shawon C.](https://wordpress.org/support/users/shawon786/)
 * (@shawon786)
 * [9 years, 7 months ago](https://wordpress.org/support/topic/create-a-shortcode-for-a-wp_query-loop/#post-8325985)
 * Hi bro,
    [http://wordpress.stackexchange.com/questions/44293/using-new-wp-query-in-shortcode-in-a-custom-field-causes-the-main-post-content-t](http://wordpress.stackexchange.com/questions/44293/using-new-wp-query-in-shortcode-in-a-custom-field-causes-the-main-post-content-t)
 * Hope it will help you 🙂

Viewing 1 replies (of 1 total)

The topic ‘Create a Shortcode for a WP_Query loop’ is closed to new replies.

## Tags

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

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 1 reply
 * 2 participants
 * Last reply from: [Shawon C.](https://wordpress.org/support/users/shawon786/)
 * Last activity: [9 years, 7 months ago](https://wordpress.org/support/topic/create-a-shortcode-for-a-wp_query-loop/#post-8325985)
 * Status: resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
