Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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

share|improve this question
    
You should try to move the filter_where() function outside of the getSomePost() function. –  birgire May 20 '13 at 17:36
    
Thank you for your help, but there is no result showing –  levat May 20 '13 at 19:08
add comment

1 Answer

up vote 1 down vote accepted

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.

share|improve this answer
    
I am confused what actually to change in this example. –  levat Jul 31 '13 at 14:01
    
@levat you cannot have multiple functions called filter_where(), name them uniquely –  Rarst Jul 31 '13 at 14:18
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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