WordPress.org

Ready to get started?Download WordPress

Forums

Time parameters and filters to show 3 random posts published last 1 month (1 post)

  1. sem8seo
    Member
    Posted 1 year ago #

    I started to work on one website after another developer and there is problem with his widget which is basically showing 3 random posts from category. There is some Switch statement which is regulating to show from category news 3 random post not older then 1 month and for other categories 3 month. I don't understand what he did there with ['interval'] but when I try to use wordpress time parameters still I don't have results and filtering. How to do it?

    function getSomePost($category,$postsPerPage=3) {
            global $post;
    
            $args = array(
                'category_name'=>$category,
                'posts_per_page'=>$postsPerPage,
                'post_type'=>'post',
                'post_status'=>'publish',
                'post__not_in'=>array($post->ID),
                'orderby'=>'rand',
            );
    
            switch ($category)
            {
                case 'news':
                    $args['interval'] = '1 MONTH';
                    break;
                case 'analysis':
                    $args['interval'] = '3 MONTH';
                    break;
                case 'reports':
                    $args['interval'] = '3 MONTH';
                    break;
            }
    
            $query = new WP_Query($args);
    
            if ( $query->have_posts() ) {
                   while ( $query->have_posts() ) {
                   $query->the_post();
                   $postThumbClass = 'no-thumb';
                   ?>
                       <div <?php post_class(array('wp-post-entry', 'sidebar-post' )); ?>>
                        <?php if(has_post_thumbnail ()):?>
                            <?php $postThumbClass = '' ?>
                            <div class="wp-post-thumbnail">
                                <a href="<?php the_permalink() ?>">
                                    <?php the_post_thumbnail(array(70,70)); ?>
                                </a>
                            </div>
                         <?php endif; ?>
    
                        <div class="wp-post-full-content <?php echo $postThumbClass ?> ">
                            <h3 class="wp-post-title"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>
    <!--
                            <div class="post-content">
                                <?php the_excerpt() ?>
                            </div>
    //-->
                        </div>
                    </div>

    I was trying to replace inside switch

    case 'news':
                    $args['interval'] = '1 MONTH';
                    break;

    with

    case 'news':
        // 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('-30 days')) . "'";
            return $where;
        }
    
        add_filter( 'posts_where', 'filter_where' );
        $query = new WP_Query( $args );
        remove_filter( 'posts_where', 'filter_where' );
        break;

    and

    case 'analysis':
                        $args['interval'] = '3 MONTH';
                        break;

    with

    case 'analysis':
    // 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('-90 days')) . "'";
        return $where;
    }
    
    add_filter( 'posts_where', 'filter_where' );
    $query = new WP_Query( $args );
    remove_filter( 'posts_where', 'filter_where' );
    break;

    but it is not working I tried also to use if ($category == 'news') but it breaks the site

Topic Closed

This topic has been closed to new replies.

About this Topic