Awesome plugin guys.
Would love to have it work on the archive page aswell, is there an obvious hack to it I’m missing?
]]>The option “orderby_last_activity” was not working for me;
but “orderby_last_comment” was.
After some debugging I fixed by quoting the the 0 in the GREATEST function call.
return $sql.', GREATEST(
‘.$wpdb->posts.’.
post_date, IFNULL(
last_comment.
date, "0")) AS
last_activity';
At this point I cannot precisely explain the reason of this (but probably with casting). On my MySQL select greatest('2013-01-01 00:00:00', 0)
return a BLOB while “0” returns the date.
Thats it, thank you for the plugin and have a good day ??
]]>how can i add the code?my theme code is
<?php
global $wp_query, $paged;
if( get_query_var( 'paged' ) ) {
$paged = get_query_var( 'paged' );
}
elseif( get_query_var( 'page' ) ) {
$paged = get_query_var( 'page' );
}
else {
$paged = 1;
}
$blog_query = new WP_Query( array( 'post_type' => 'post', 'paged' => $paged ) );
$temp_query = $wp_query;
$wp_query = null;
$wp_query = $blog_query;
if ( $blog_query->have_posts() ) :
while ( $blog_query->have_posts() ) : $blog_query->the_post();
?>
]]>
[Sun May 13 21:26:28 2012] [error] [client x] PHP Fatal error: Call to a member function get() on a non-object in /home/www/x/wp-includes/query.php on line 27, referer: https://localhost:8888/x/
For some reason WP_Query is not liking the OOP nature of this module, specifically when another module is modifying the query args?
Here is the code simplified which now works for my use case (Was getting these errors when combined with another plugin which modified posts_where) and was unable to remove_filter() these… because of &$this::posts_where().
<?php
/*
Plugin Name: Filter By Comments
Description: Adds additional query vars (<code>comment_count</code>, <code>comment_count_compare</code>, <code>orderby_last_comment</code>) for filtering posts by comments.
Version: 1.0
Author: Kevin Ingersoll
Author URI: https://keviningersoll.com/
*/
add_filter('query_vars', 'FilterByComments_query_vars');
add_filter('posts_fields', 'FilterByComments_posts_fields');
add_filter('posts_where', 'FilterByComments_posts_where');
add_filter('posts_join', 'FilterByComments_posts_join');
add_filter('posts_orderby', 'FilterByComments_posts_orderby');
function FilterByComments_query_vars($vars) {
$vars[] = 'comment_count';
$vars[] = 'comment_count_compare';
$vars[] = 'orderby_last_comment';
$vars[] = 'orderby_last_activity';
return $vars;
}
function FilterByComments_posts_fields($sql) {
global $wpdb;
if(get_query_var('orderby_last_activity')) {
// might be faster, but inaccurate when post date is altered, i.e. future-publish posts
//return $sql.', IFNULL(<code>last_comment</code>.<code>date</code>, <code>'.$wpdb->posts.'</code>.<code>post_date</code>) AS <code>last_activity</code>';
return $sql.', GREATEST(<code>'.$wpdb->posts.'</code>.<code>post_date</code>, IFNULL(<code>last_comment</code>.<code>date</code>, 0)) AS <code>last_activity</code>';
}
return $sql;
}
function FilterByComments_posts_where($sql) {
global $wpdb;
if(is_numeric($count = get_query_var('comment_count'))) {
$compare = get_query_var('comment_count_compare');
return ($sql !== '' ? $sql.' AND ' : '').$wpdb->prepare('<code>'.$wpdb->posts.'</code>.<code>comment_count</code> '.(in_array($compare, array('!=', '>', '>=', '<', '<=')) ? $compare : '=').' %d', $count);
}
return $sql;
}
function FilterByComments_posts_join($sql) {
global $wpdb;
if(get_query_var('orderby_last_comment') || get_query_var('orderby_last_activity')) {
return $sql.'
LEFT JOIN (SELECT <code>comment_post_ID</code> AS <code>post</code>, MAX(<code>comment_date</code>) AS <code>date</code> FROM <code>'.$wpdb->comments.'</code> WHERE <code>comment_approved</code>="1" GROUP BY <code>post</code>) AS <code>last_comment</code>
ON <code>last_comment</code>.<code>post</code> = <code>'.$wpdb->posts.'</code>.<code>ID</code>
';
}
return $sql;
}
function FilterByComments_posts_orderby($sql) {
if(get_query_var('orderby_last_comment')) {
return '<code>last_comment</code>.<code>date</code> DESC'.($sql !== '' ? ', '.$sql : '');
}
elseif(get_query_var('orderby_last_activity')) {
return '<code>last_activity</code> DESC'.($sql !== '' ? ', '.$sql : '');
}
return $sql;
}
?>
For those interested… Original author, perhaps you can shed more light on this as I’m just a hobbyists and not a lot of understanding with OOP.
Also I believe this plugin should somehow later remove_filter() what it adds so as not to break other plugins… but no idea how to automatically handle that after the get_posts() in index.php or elsewhere – I guess must be left to the site owner at this point.
]]>Working great for the front page but when visiting other pages from the pagination, the front page is loaded again.
Great idea for a module, thanks for sharing. ??
]]>Hi,
Can you confirm me that your plugin do that (because it doesn’t work)
I want to order my posts by the date of post if there is no comment or by the date of the last comment if there is comment.
I understand that I have to use orderby_last_activity to do that.
I use like that :
<?php query_posts( ‘orderby_last_activity=1&paged=’ . $paged); ?>
<?php if (have_posts()) : ?>
<?php while (have_posts()) : the_post(); ?>
But it doesn’t work ??
Thanks for your help and sorry for my english
I’ve installed the plugin and now the post with the most recent comment appears first on my index. However, I would like it to be ordered by the most recent comment or post. ie. The most recent thing to be posted, whether it is a post or a comment on a post, that post should appear first. For example:
#1 Post-C (posted at 15:00, no comments)
#2 Post-A (posted at 12:00, most recent comment at 14:00)
#3 Post-B (posted at 13:00, no comments)
Post-A appears above Post-B because Post-A has a comment posted since the most recent thing in Post-B. Post-C appears above Post-A because it is more recent.
Currently, using this plugin they would appear like this:
#1 Post-A (posted at 12:00, most recent comment at 14:00)
#2 Post-C (posted at 15:00, no comments)
#3 Post-B (posted at 13:00, no comments)
Like this, a new post never gets to the top of the index unless it gets a comment.
]]>Hi!
I installed the plugin but I could not get it work in the Loop…
After activation, in my index.php, I wrote:
< ?php get_header(); ? >
< div id="col1" >
< ?php
query_posts('orderby_last_comment', $paged);
if (have_posts()) : while (have_posts()) : the_post(); ? >
And it seems to ignore the code, my posts are not order by last commented, there are ordered by publish date.
If it’s any help, I use this theme
Sorry for my english…
]]>I don’t have the means to test this plugin on older versions of PHP and WordPress. If you happen to have anything older than WP 3.1.3 and PHP 5.3, please let me know if the plugin works!
]]>