Shortcode: Execute Multiple Custom Post Type Queries using WordPress Shortcodes

Add following snippet to “functions.php”

Snippet

add_shortcode('customPost', 'customPostType_shortcode');
function customPostType_shortcode()
{
extract(shortcode_atts(array(
        'type' => 'post',
        'limit' => '10',
        ),$atts));
 
        //The Query
    query_posts('post_type='.$type.'&showposts='.$limit);
 
        //The Loop
       if ( have_posts() ) : while ( have_posts() ) : the_post();
                echo  "<h3><a href=\"".the_permalink($post->ID)."\">".get_the_title($post->post_title)."</a></h3>";
                echo  the_excerpt();
        endwhile; else:
    endif;
 
        //Reset Query
    wp_reset_query();
}

Usage

[customPost type=movies,books limit=5]

Courtesy: GraphicBeacon

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>