【问题标题】:Popular Post with wp_query and date_query wordpress带有 wp_query 和 date_query wordpress 的热门帖子
【发布时间】:2017-07-25 13:14:53
【问题描述】:

我正在尝试在我的网站上创建一个热门帖子部分,以显示过去一周最受欢迎的 5 个帖子。

热门帖子的功能

function shapeSpace_popular_posts($post_id) {
    $count_key = 'popular_posts';
    $count = get_post_meta($post_id, $count_key, true);
    if ($count < 1) {
        delete_post_meta($post_id, $count_key);
        add_post_meta($post_id, $count_key, '0');
    } else {
        $count++;
        update_post_meta($post_id, $count_key, $count);
    }
}
function shapeSpace_track_posts($post_id) {
    if (!is_single()) return;
    if (empty($post_id)) {
        global $post;
        $post_id = $post->ID;
    }
    shapeSpace_popular_posts($post_id);
}
add_action('wp_head', 'shapeSpace_track_posts');

循环查找热门帖子

<?php $popular = new WP_Query(array(
                    'posts_per_page'=>5,
                    'date_query'          => array(
                        //set date ranges with strings!
                        'after' => '1 week ago',
                        'before' => 'today',
                        //allow exact matches to be returned
                        'inclusive'         => true,
                    ),
                    'meta_key'=>'popular_posts',
                    'orderby'=>'meta_value_num',
                    'order'=>'DESC'));
                    while ($popular->have_posts()) : $popular->the_post(); ?>
                    <h5 class="section_category">
                        <?php the_category(', ') ?>
                    </h5>
                    <div class="trending_title">
                        <?php the_title( sprintf( '<h4><a href="%s" rel="bookmark">', esc_url( get_permalink() ) ), '</a></h4>' ); ?>
                    </div>
                    <h5 class="section_author">
                        By <?php coauthors_posts_links(); ?>
                    </h5>
                    <div class="border-bottom"></div>
                    <?php endwhile; wp_reset_postdata(); ?>

如果你能帮上忙,我会很感激的。

谢谢,

【问题讨论】:

    标签: php wordpress


    【解决方案1】:

    使用“query_posts”而不是“WP_Query”

    <?php
    $args = array(
        'post_type'  => 'post',
        'showposts' => 5,
        'date_query'          => array(
            //set date ranges with strings!
            'after' => '1 week ago',
            'before' => 'today',
            //allow exact matches to be returned
            'inclusive'         => true,
        ),
        'meta_key' => 'popular_posts',
        'orderby' => 'meta_value_num',
        'order'   => 'DESC'
    );
    query_posts($args);
    //query_posts('post_type=post&showposts=5&meta_key=popular_posts&orderby=meta_value_num&order=DESC');
    if (have_posts()) : 
    ?>
    <?php
    while (have_posts()) : the_post();
    ?>
         <h5 class="section_category">
            <?php the_category(', ') ?>
        </h5>
         <div class="trending_title">
            <?php the_title( sprintf( '<h4><a href="%s" rel="bookmark">', esc_url( get_permalink() ) ), '</a></h4>' ); ?>
        </div>
        <h5 class="section_author">
            By <?php coauthors_posts_links(); ?>
        </h5>
        <div class="border-bottom"></div>
    <?php
    endwhile; 
    ?>
    <?php
    endif;
    wp_reset_query();
    ?>
    

    【讨论】:

    • 当我用这个替换时似乎没有什么不同。有什么建议吗?
    • 在 date_query 之前一切正常,然后它似乎没有响应,我不确定为什么
    • 你可以试试 'column' => 'post_date', 'after' => '- 7 days',而不是 'after' => '1 周前',
    猜你喜欢
    • 2013-10-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-19
    • 1970-01-01
    • 2017-01-21
    • 1970-01-01
    相关资源
    最近更新 更多