23

I usually use this approach: wrong approach <?php query_posts( array( 'category_name' => 'news', 'posts_per_page' => 3, )); ?> <?php if( have_posts() ): while ( have_posts() ) : the_post(); ?> <?php the_excerpt(); ?> <?php endwhile; ?> <?php else : ?> <p><?php __('No News'); ?></p> &...


18

Have a look in wp-includes\default-widgets.php. /** * Recent_Posts widget class * * @since 2.8.0 */ class WP_Widget_Recent_Posts extends WP_Widget { function __construct() { $widget_ops = array('classname' => 'widget_recent_entries', 'description' => __( "The most recent posts on your site") ); parent::__construct('recent-...


9

Don't use the 'helper' methods, they tend to cause more trouble than they're worth. Any time you want to grab posts, be it the latest, the oldest, in a category etc, use a WP_Query loop, here is its standard form: $query = new WP_Query( $args ); if ( $query->have_posts() ) { while ( $query->have_posts() ) { $query->the_post(); /...


8

The default Recent Posts Widget code is in includes/default-widgets.php but you should not be hacking Core code. Copy that function to your theme's functions.php, rename it, and create your own customized widget.


6

your problem is using author_email, you need user_id: i just use similar script. <?php $args = array( 'user_id' => get_the_author_meta('ID'), 'number' => 10, // how many comments to retrieve 'status' => 'approve' ); $comments = get_comments( $args ); if ( $comments ) { $output.= "<ul&...


6

HERE IS THE SOLUTION /** * Extend Recent Posts Widget * * Adds different formatting to the default WordPress Recent Posts Widget */ Class My_Recent_Posts_Widget extends WP_Widget_Recent_Posts { function widget($args, $instance) { if ( ! isset( $args['widget_id'] ) ) { $args['widget_id'] = $this->id; ...


5

You can exclude posts from the recent posts widget via the widget_posts_args filter: add_filter( 'widget_posts_args', 'exclude_posts_wpse_103570'); function exclude_posts_wpse_103570( $args ){ // post ID's to exclude: $args['post__not_in'] = array( 123, 234, 345 ); return $args; }


5

Give it a try works perfect for me what it is doing query get the all the posts with a left jon with comments table so when a post has comment then it also has the comment_date if no comments posted on the post then in result set it will be null so i have merged the comment_date with post_date so which post has the greater date (for comment_date or ...


5

The arguments you are using are wrong. They should be: $args = array( 'numberposts' => '10', 'post_type' => 'post', 'post_status' =>'publish', 'tax_query' => array( 'taxonomy' => 'category', 'field' => 'id', 'terms' => array( 10, 11, 57 ),...


5

You need to get the post ids from the original main query and exclude them. Then you should remove your offset from your custom query. That should do the trick. Random ordering basically ignores the offset parameter, so you need to explicitely remove the posts from the query to exclude them You can use wp_list_pluck() to get an array of post ids from the ...


5

I believe that you can achieve this effect by grouping the query by author ID, which will require a filter(mostly cribbed from the Codex): function my_posts_groupby($groupby) { global $wpdb; $groupby = "{$wpdb->posts}.post_author"; return $groupby; } add_filter( 'posts_groupby', 'my_posts_groupby' ); If you have less than 8 authors, however, ...


5

Here's one way to do it through the the_title filter. We can limit the scope to the Recent Posts widget, by initialize it within the widget_posts_args filter and then remove it again after the loop. /** * Recent Posts Widget: Append Thumbs */ add_filter( 'widget_posts_args', function( array $args ) { add_filter( 'the_title', 'wpse_prepend_thumbnail', ...


4

The problem was that the SQL query code was getting the term_taxonomy_id, not the actual tag ID. I added an additional INNER JOIN using the term_taxonomy table to get the term_id. This seems to work, but if a mod can improve this, please do! <ul id="footer-tags"> <?php $wpdb->show_errors(); ?> <?php global $wpdb; $term_ids = $wpdb->...


4

It kind of depends on what you're going for. If you want to do a "page of posts" -- other words, create a new page template file -- you can create a secondary loop on that page. The codex has an example of this and here's another, very stripped down example. <?php /* Template Name: Page of Posts */ get_header(); ?> <?php while( have_posts() ): ...


4

You could use your list of blog ids in this way ... $posts = array(); foreach ( $your_list_of_blog_ids as $blog_id ) { switch_to_blog( $blog_id ); $query = new WP_Query( array( 'post_type' => 'any', 'posts_per_page' => 10, ) ); while ( $query->have_posts() ) { $query->next_post(); ...


4

The original author isn't quite right in saying "which is merely the more elegant way to write". set_query_var() will always override the main query, whereas if you actually use: $wp_query->set( 'category__not_in', $excluded ); ... it will work for any instance of query_posts(), such as the recent posts widget.


4

This code could work, I'm using the setup_postdata to create a post object similar to what the function the_post() does, so now you can use the functions you have in a Loop. $recent_posts = wp_get_recent_posts( array( 'numberposts' => 3, 'orderby' => 'post_date', 'order' => ...


4

This is a default feature which by default is set not to show the post date. Here is the part of the code responsible to show/hide the post date <?php if ( $show_date ) : ?> <span class="post-date"><?php echo get_the_date(); ?></span> <?php endif; ?> To enable the post date, just check the Display post date? checkbox EDIT ...


3

Compare the current time with the UNIX time stamp of the post date: // now minus one day in seconds if ( ( time() - 86400 ) < get_the_date( 'U' ) ) { echo '<span class="new">New!</span>'; }


3

There's a guide for this precise case at the wordpress codex. See it here: I paste the code here because it's quite short, for more information go to the wordpress.org site. <?php $args = array( 'numberposts' => 10, 'order'=> 'ASC', 'orderby' => 'title' ); $postslist = get_posts( $args ); foreach ($postslist as $post) : setup_postdata($post); ?&...


3

Inside your post loop: <div class="recent-post"> <div class="thumbnail"> <?php if(has_post_thumbnail()) { the_post_thumbnail(); } else { // show a default image if no featured image is specified echo '<img src="'.get_bloginfo("template_url").'/images/img-default.png" />'; ...


3

There are a few ways to do this, my first thought is to do: $query = new WP_Query(array( 'posts_per_page' => 1, 'paged' => 5, 'post_type' => '[your_content_type]', )); while ($query->have_posts()) : $query->the_post(); // loop stuff endwhile;


3

First of all learn WP_Query class. Answering on questions: (this question) How do I grab the nth element of a content type? eg. always getting the 1st or 5th most recent element from the db. $query = new WP_Query( 'post_type=special_stuff&posts_per_page=1&paged=5' ); List item // The Query $the_query = new WP_Query( $args ); // The Loop ...


3

You can see everything returned via php's var_dump or print_r: $recent_posts = wp_get_recent_posts(); echo '<pre>'; print_r( $recent_posts ); echo '</pre>'; This will reveal that the date is stored in post_date: foreach( $recent_posts as $recent ): echo $recent['post_date']; endforeach; Or to format the date, use php's date and strtotime: ...


3

Try: $postslist = get_posts( 'orderby=ID&numberposts=15' ); Here, we're ordering by the ID, which should relate to when the posts were added to the database.


3

You can use the wp_trim_excerpt() to get the excerpt of the content. You can use it inside your loop to get the excerpt of each post the string provided to a maximum of 55 words if it is more then a [...] will be added to the end of the string. <?php $recent_posts = wp_get_recent_posts(array('post_type'=>'jokes')); foreach( $recent_posts ...


3

The post_excerpt value is empty because you have no explicit excerpts for your posts. While the_excerpt() does generate an excerpt from the posts content if the post excerpt is empty, the function wp_get_recent_posts(), which is basically a wrapper for get_posts(), doesn't.


3

I would take a different approach to this using a direct database query to get a list of author IDs ordered by recent posts. global $wpdb; $user_ids = $wpdb->get_results( " SELECT DISTINCT post_author FROM $wpdb->posts ORDER BY post_date DESC " ); if ( $user_ids ) { foreach ( $user_ids as $user_id ) { $user = ...


3

Your problem is your custom query. Do not use variables that is used by default by Wordpress. $posts and $post is Wordpress reserved global variables. Change $post to a unique variable, something like $posts_query. When using get_posts and you need to setup postdata to make use of template tags, you have no choiche but to use $post as setup_postdata() ...


3

The core of the problem is that you set cookies when the content is being accessed, not when it is read. Browsers can try to prefetch content, and your description sound like the browser is prefecthing the "next" link found in the header. From experience, you should avoid setting cookies on the server and prefer setting them on client side (add to the html ...


Only top voted, non community-wiki answers of a minimum length are eligible