This document discusses different ways to get and display different sets of posts in WordPress themes using functions like query_posts(), get_posts(), and the main loop. It covers getting the most commented posts, featured posts, recent posts from categories, most read posts, and more. It also discusses displaying thumbnails and subcategory listings.
Getting Different Post Sets in WordPress with query_posts(), get_posts(), and Custom Fields
1. Getting different sets of
posts in your theme
Roman Rus
@RomSocial
www.websitesupgrade.com
2. The Main Loop
The loop:
while (have_posts()) :
the_post();
the_permalink();
the_post_thumbnail();
the_content();
the_excerpt();
the_time();
the_author();
comments_template( );
endwhile;
3. query_posts()
The loop:
while (have_posts()) :
the_post();
...
endwhile;
Second time same loop:
rewind_posts();
while (have_posts()) :
the_post(); ...
endwhile;
Run a different loop:
query_posts(
'category_name=cat2&
posts_per_page=10');
while (have_posts()) :
the_post(); ...
endwhile;
wp_reset_query();
4. get_posts()
Most commented posts:
$args = array(
'numberposts' => 4,
'orderby' => ‘comment_count',
'order' => 'DESC');
$theposts = get_posts( $args);
If in functions.php etc.:
global $post;
foreach($theposts as $post) :
setup_postdata($post);
the_permalink();
the_post_thumbnail();
the_title();
...
endforeach;
wp_reset_postdata();
5. Most commented posts
Featured posts
Most recent posts from
selected categories, then
second most recent
posts, etc.
Most read posts
Most shared on social
media posts
List of subcategories with
images
7. How it is done?
Most read posts
Most commented posts
Featured posts
Most recent post, second
most recent post, etc.
from individual
categories
Plugin: Meta Box
Define custom field,
++ on single post views
or
Plugin: WP-PostViews
Most shared on social
media posts
Plugin: Most Shared Posts
Name of meta tag
'meta_key' =>
'_msp_total_shares'
Sort by
'comment_count'
or 'date' (default)
use
'offset', 'category',
'numberposts'
Sort by 'date' (default)
with filter
'meta_key' => '_fp',
'meta_value' => '1',
Sort by 'meta_value'
or 'meta_value_num'