Loop Through Custom Post Types

9 views
Skip to first unread message

namtrok

unread,
Feb 27, 2015, 10:52:57 AM2/27/15
to wp...@googlegroups.com
I have a Custom Post Type (via the Wiki plugin from WPMU Dev)

It works just fine in it's own environment, but I'd like to loop through it and display the 5 most recently updated Wiki pages (CPT). 

How do I loop through CPT for display? Should I go with a widget and add a new sidebar area? Do I need a new plugin or can I do this from a text widget? Can I create my own shortcode to insert this loop?

I'm sure it'd be easier to do this via a theme file but I need it displayed on pages in the middle of page content. Unless I create a shortcode (which I have no idea how to do ;) I don't see a more simple way to accomplish this.

Thanks for your help!

Matt Meade

unread,
Feb 27, 2015, 11:18:25 AM2/27/15
to WPGR

It's just like writing your own custom loop except the post type will be your custom post type instead of "post".

I'll post code when I get back to my desk

--
You received this message because you are subscribed to the Google Groups "WordPress Grand Rapids (WPGR)" group.
To unsubscribe from this group and stop receiving emails from it, send an email to wpgr+uns...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Matt Meade

unread,
Feb 27, 2015, 11:34:11 AM2/27/15
to WPGR

Here is how to loop through them:
http://codex.wordpress.org/Template_Tags/get_posts

Just change this line:
'post_type'        => 'post',
To:
'post_type'        => 'your_custom_post_type',

There are examples further down the page on how to loop through them and output the data.

You should be able to do this as a shortcode to place in content.

Here is how you create shortcodes:
http://codex.wordpress.org/Shortcode_API

Topher

unread,
Feb 27, 2015, 3:11:13 PM2/27/15
to wp...@googlegroups.com
Check this out:

http://wpgr.org/2013/04/21/storing-complex-queries-in-transients/

That code creates a shortcode which loops through a CPT getting a
specified number, and bonus, stores it in a caching transient for speed.

The loop part is calling a template part, which is like a php include.

Let me know if you get stuck.

Topher

namtrok

unread,
Feb 27, 2015, 9:32:33 PM2/27/15
to wp...@googlegroups.com
Sorry Topher, that was way over my head! But I did become smarter by trying to figure it out!

Here's what I ended up using:

//[wikilist]
function wikilist_func( $atts ){

 $output = '';
global $post;

$output .= '<ul class="wikilist">';

$args = array( 'posts_per_page' => 7, 'post_type'=> 'incsub_wiki', 'orderby' => 'modified', 'order' => 'DESC' );

$myposts = get_posts( $args );
foreach ( $myposts as $post ) : setup_postdata( $post ); 
        $output .= '<li><a href="' . get_the_permalink($post->id) . '">' . get_the_title($post->id)  . '</a></li>';
endforeach; 
wp_reset_postdata();
$output .= '</ul>';
return $output;

}
add_shortcode( 'wikilist', 'wikilist_func' );


It works for my purposes! Thanks Matt and Topher!
Reply all
Reply to author
Forward
0 new messages