仅从最新版本随机排序 WordPress 上的帖子

问题描述 投票:0回答:1

在我的相关帖子中,我想随机排序我已经存档的内容,但我只想显示特定时期的内容。例如:从过去 3 个月到现在。

这是我的相关邮政编码:

<?php
$categories = wp_get_post_terms( get_queried_object_id(), 'category', ['fields' => 'ids'] );
$args = [
    'post__not_in'        => array( get_queried_object_id() ),
    'posts_per_page'      => 8,
    'ignore_sticky_posts' => 1,
    'orderby'             => 'rand',
    'tax_query' => [
        [
            'taxonomy' => 'category',
            'terms'    => $categories
        ]
    ]
];
$my_query = new wp_query( $args );
if( $my_query->have_posts() ) {
    echo '<div id="related"><h4>Related Posts</h4>';
        while( $my_query->have_posts() ) {
            $my_query->the_post(); ?>
            <div class="ht_grid_1_4 box-effect">    

    <?php if ( has_post_thumbnail() ) { ?>
        <a class="thumbnail-link" href="<?php the_permalink(); ?>">
            <div class="thumbnail-wrap">
                <?php 
                    the_post_thumbnail('justvideo_post_thumb'); 
                ?>
            </div><!-- .thumbnail-wrap -->
            
        </a>
    <?php } ?>  

    <div class="entry-header">


        <h2 class="entry-title"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>

        
                                    
    </div><!-- .entry-header -->
                </div>
        <?php }
        wp_reset_postdata();
    echo '</div><!--related-->';
}
?> 

我可以随机显示帖子,我只需要你帮我让它显示特定时期的内容。

php wordpress-theming
1个回答
0
投票

我希望这可以帮助你(date_query):

$args = [
  'post__not_in'        => array( get_queried_object_id() ),
  'posts_per_page'      => 8,
  'ignore_sticky_posts' => 1,
//add this:
//==========================================================
//==========================================================
  'date_query' => array(
    array(
      'after'     => date("Y-m-d", strtotime('-3 months')),
      'before'    => date("Y-m-d", strtotime('now')),
      'inclusive' => true,
    ),
   ),
//==========================================================
//==========================================================
  'orderby'             => 'rand',
  'tax_query' => [
    [
        'taxonomy' => 'category',
        'terms'    => $categories
    ]
  ]
];
© www.soinside.com 2019 - 2024. All rights reserved.