Wordpress wp#u上个月的查询+;分拣站

Wordpress wp#u上个月的查询+;分拣站,wordpress,Wordpress,我正在使用WordPress插件“我推荐这个”,它允许人们“喜欢”帖子。 该值存储为一个元键,我们可以查询它以生成“最推荐”页面 插件-> 循环 <?php query_posts('&orderby=meta_value&meta_key=_recommended'); if (have_posts()): while (have_posts()) : the_post(); ?> <article &

我正在使用WordPress插件“我推荐这个”,它允许人们“喜欢”帖子。 该值存储为一个元键,我们可以查询它以生成“最推荐”页面

插件->

循环

<?php 
        query_posts('&orderby=meta_value&meta_key=_recommended');
        if (have_posts()): while (have_posts()) : the_post();
    ?>
        <article <?php post_class('item-post block'); ?> id="post-<?php the_ID(); ?>">


         <h2><?php the_title(); ?></h2>
        </article>


    <?php endwhile; ?>
    <?php endif; ?>

这很有效,它可以找到最推荐的帖子

两个问题

  • 我如何将返回的帖子限制在一个日期范围内,比如说,过去3个月

  • 类似地,我怎么能有一个“本周最受欢迎”的按钮,允许用户查看过去7天的帖子过滤结果


  • 您可以尝试这样的方法,这只是一个基本示例:

    $today = getdate();
    $threemonths = $today["mon"]-3;
    
    $args = array(
        'date_query' => array(
            array(
                'after'     => array(
                    'year'  => $today["year"],
                    'month' => $threemonths,
                    'day'   => $today["mday"],
                ),
                'before'    => array(
                    'year'  => $today["year"],
                    'month' => $today["mon"],
                    'day'   => $today["mday"],
                ),
                'inclusive' => true,
            ),
        ),
        'posts_per_page' => -1,
    );
    $query = new WP_Query( $args );
    
    他们在wordpress文档中进一步细分了它:


    我算出来了。与WP_查询数组一起使用自定义筛选器

    <?php 
    
                    // Create a new filtering function that will add our where clause to the query
                    function filter_where( $where = '' ) {
                        // posts in the last 30 days
                        $where .= " AND post_date > '" . date('Y-m-d', strtotime('-180 days')) . "'";
                        return $where;
                    }
    
                    add_filter( 'posts_where', 'filter_where' );
    
                    $featuredPosts = new WP_Query( array(
    
                    'meta_key'=>'_recommended',  
                    'orderby' => 'meta_value_num', 
                    'order' => DESC
                    ) ); 
    
    
                    remove_filter( 'posts_where', 'filter_where' ); ?>
    
    
                    <?php if ( $featuredPosts->have_posts() ) : ?>
    
    
    
                    <?php while ( $featuredPosts->have_posts() ) : $featuredPosts->the_post(); ?>
    
                    <article <?php post_class('item-post block'); ?> id="post-<?php the_ID(); ?>">
    
                    <div class="figure">
                    <?php the_post_thumbnail('frontthumb'); ?>
                    </div>
    
                <div class="fig-cover">
    
                    <div class="fig-bottom">
                            <div class="title-container">
    
    
                              <?php if ( get_post_meta($post->ID, 'Price', true) ) { ?>
    
                                   <div class="price-container">
    
                                        <?php echo get_post_meta($post->ID, "Price", true); ?>
    
                                   </div>
                                   <h2 class="price-title"><?php the_title(); ?> </h2>
    
    
    
    
    
                                    <?php } else { ?>
                                     <h2><?php the_title(); ?> </h2>
                                    <?php } ?>
    
    
    
    
                        </div> <!-- end div title-container -->
                    </div>
    
                </div>
    
                <div class="reco">
                <?php if( function_exists('dot_irecommendthis') ) dot_irecommendthis(); ?>
                </div>
    
                <a class="the-post-link" href="<?php the_permalink(); ?> ">
    
    
                </a>
                </article> <!-- end div post -->
    
    
    
                    <?php endwhile; wp_reset_query(); ?>
    
    
                    <?php endif; ?>