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.

While tweaking the "Bryans Childpage Thumbnail Generator" plugin, I am applying filter (with pagination) to the get_posts for WP Pages (post_type=page), but its not filtering at all. Here is the code:

extract(shortcode_atts(array(
    'order'      => 'ASC',
    'orderby'    => 'title',
    'id'         => '',
    'number'     => -1,
    'height'    => '100',
    'width'     => '',
    'size'       => 'thumbnail',
    'style'     => '',
    'include'    => '',
    'pagetitle'  => false,
    'selector'   => 'h2',
    'selector_height' => '30px',
    'selector_width' => '100%',
    'selector_padding' => '',
    'showimages' => true,
    'text_align' => 'left',
    'text_margin' => '',
    'exclude'    => '',
    'posts_per_page'  => '5',
    'page_number'   => '1',
    'filter_year' => '0'
), $attr));

if( $id == '')
{
    $id = $post->ID;
}

$id = intval($id);

if($width != null)
{
    $width='width:' . $width . 'px';
}

if ($order == 'RAND')
{
    $orderby = 'none';
}

if($text_margin == '')
{
    $textmargin = ($height/2)-10;
    $textmargin .= "px 0 0 0";

} else 
{
    $textmargin = $text_margin;
}

$offsettext = ($height*3);

$offset = ($page_number - 1) * $posts_per_page;

$offset = $offset < 1 ? 0 : $offset;

function filter_where( $where = '' ) {

    if($filter_year > 0)
        $where .= " AND post_date >= '" . $filter_year . "-01-01' AND post_date < '" . ($filter_year + 1) . "-01-01'";

        // e.g. AND post_date >= '2011-01-01' AND post_date < '2012-01-01'

        return $where;
}

add_filter( 'posts_where', 'filter_where' );

$countposts = count(get_posts(array(
    'suppress_filters' => false,
    'post_parent' => $id,
    'post_type' => 'page',
    'post_status' => 'publish',
    'numberposts' => $number,
    'exclude' => $exclude,
    'orderby' => $orderby
)));


$childpages = & get_posts(array(
    'suppress_filters' => false,
    'post_parent' => $id,
    'post_type' => 'page',
    'post_status' => 'publish',
    'numberposts' => $number,
    'exclude' => $exclude,
    'orderby' => $orderby,
    'order' => $order,
    'posts_per_page' => $posts_per_page,
    'offset' => $offset
));

remove_filter( 'posts_where', 'filter_where' );

$totalpages = $countposts / $posts_per_page;

$totalpages = intval($totalpages) + ($totalpages > intval($totalpages) ? 1 : 0);

And here is the shortcode calling this plugin:

<?php echo do_shortcode("[gallery_childpages number='-1' height='220' width='295' size='medium' style='armthumbs' include='' pagetitle='1' selector='H2' showimages='0' showimages='true' exclude='1,33,22' posts_per_page = '9' page_number='" . get_query_var('page') . "' filter_year='" . $_GET['fyear'] . "']"); ?>

The pagination is working but filter isn't..

share|improve this question
    
Remove the extract() and assign the result to a var. Then replace all the instances with those var contents. This will show you where it fails. extract() hides errors from users and the IDE. –  kaiser Sep 12 '13 at 12:42
add comment

2 Answers

up vote 1 down vote accepted

According with Wordpress documentation, the post_where filter will not affect to the query generated by the get_posts() function. See also the default usage of get_posts() where you can see the argument suppress_filters set to true by deafult. In order to run post_where filter in the query generated by get_posts() function you have to set suppress_filters argument to false:

get_posts( array( 'suppress_filters' => FALSE ) );

So, you code could be something like:

$countposts = count(get_posts(array(
                   'suppress_filters' => false,
                   'post_parent       => $id,
                   'post_type'        => 'page',
                   'post_status'      => 'publish',
                   'numberposts'      => $number,
                   'exclude'          => $exclude,
                   'orderby'          => $orderby
             )));

Also, you have a lot of variables not initializaed like, $id, $number, $exclude, $orderby or $filter_year.

share|improve this answer
    
I have updated the question with initialization code and added 'suppress_filters'. Unfortunately, its still not working. –  vulcan raven Sep 12 '13 at 11:09
1  
I your function filter_where() you are trying to read the value of $filter_year, but this var is out of the scope of the function, i.e., the var $filter_year is not defined inside your function. As the var is defined in the global scope, you can try to define global $filter_year inside the function. –  cybnet Sep 12 '13 at 11:50
    
+1 thanks. global didn't worked but $GLOBALS['p_year'] = $filter_year; did the trick! –  vulcan raven Sep 12 '13 at 14:12
add comment

It looks like this condition is never fullfilled:

if($filter_year > 0)

since you haven't defined the variable $filter_year.

share|improve this answer
    
I didn't showed the initialization code. Check the updated question. –  vulcan raven Sep 12 '13 at 11:09
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.