In a Wordpress template I have a index-file, which shows some blog posts. The query is not there (but I guess it can just be overwritten), but the blogposts are showed through have_posts(), while-loop etc.
It uses some pagination as well.
I want the blog posts to be sorted by a meta key belonging to the author of the posts. I found a solution to do that with this
$querystr = "
SELECT wposts.*
FROM $wpdb->posts wposts, $wpdb->usermeta umeta
WHERE wposts.post_author = umeta.user_id
AND umeta.meta_key = 'wp-last-login'
AND wposts.post_type = 'post'
AND wposts.post_status = 'publish'
ORDER BY umeta.meta_value DESC
";
$pageposts = $wpdb->get_results($querystr, OBJECT);
and then make a foreach loop.
That works fine, but it messes up the pagination.
Therefore I would like help to find out how to either get the pagination to work with the foreach-method or get a way to sort the actual code like above but with the normal query-method that is used by have_posts(), while-query etc. A overview of original code (but without the query) is here.
<?php if (have_posts()) : ?>
<?php $count = 0; while (have_posts()) : the_post(); $count++; ?>
<!-- Show posts -->
<?php endwhile; ?>
<?php
$next_page = get_next_posts_link(__('Next', 'Destro'));
$prev_pages = get_previous_posts_link(__('Previous', 'Destro'));
if(!empty($next_page) || !empty($prev_pages)) :
?>
<div class="pagination">
<?php if(!function_exists('wp_pagenavi')) : ?>
<div class="al"><?php echo $prev_pages; ?></div>
<div class="ar"><?php echo $next_page; ?></div>
<?php else : wp_pagenavi(); endif; ?>
</div><!-- /pagination -->
<?php endif; ?>
etc...
---- EDIT -----
I have tried to do this now, but it doesn't seem to work. It's inserted just before have_posts() and the loop starts
function joinPostmeta($join) {
global $wp_query, $wpdb;
$join .= "LEFT JOIN $wpdb->usermeta umeta ON $wpdb->posts.post_author = umeta.user_id";
return $join;
}
function wherePostmeta($where) {
global $wp_query, $wpdb;
$where .= "AND umeta.meta_key = 'wp-last-login'";
return $where;
}
function orderbyPostmeta($orderby_statement) {
global $wp_query, $wpdb;
$orderby_statement .= "umeta.meta_value DESC'";
return $orderby_statement;
}
add_filter('posts_join', 'joinPostmeta');
add_filter('posts_where', 'wherePostmeta');
add_filter('posts_orderby', 'orderbyPostmeta');