dongqianchi0512 2014-02-18 16:10
浏览 59

Wordpress查询 - foreach / while

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');
  • 写回答

1条回答 默认 最新

  • dream_high1026 2014-02-18 16:30
    关注

    You should stick to using WP_Query like a normal WordPress loop, but use the posts_join, posts_where, and posts_orderby filters to alter the query it generates so it queries against the user metadata like you have in your query.

    This will allow WP_Query to apply its usual pagination rules and other behavior.

    评论

报告相同问题?

悬赏问题

  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用