2

I started to work on one website after another developer and there is problem with his "popular 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 use wordpress time parameters still I don't have results.

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

2
  • You should try to move the filter_where() function outside of the getSomePost() function.
    – birgire
    Commented May 20, 2013 at 17:36
  • Thank you for your help, but there is no result showing
    – levat
    Commented May 20, 2013 at 19:08

1 Answer 1

1

You cannot declare multiple functions with same name in PHP. Codex examples for this are technical demonstration, rather than ready-made usable snippet.

Give your filter functions unique names, that are not likely to clash with your own or third party code.

2
  • I am confused what actually to change in this example.
    – levat
    Commented Jul 31, 2013 at 14:01
  • @levat you cannot have multiple functions called filter_where(), name them uniquely
    – Rarst
    Commented Jul 31, 2013 at 14:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.