0

I'm creating a featured post slider which takes 4 pages or posts from the 'featured' category. I'm trying to use WP_Query, but am having no joy :(

my slider query is:

$ml_featured = new WP_Query( 

    array(
        'posts_per_page' => 4,
        'post_type' => array('page', 'post'),
        'category__in' => 22
    )
);


if( $ml_featured->have_posts() ) {
    while( have_posts() ) : $ml_featured->the_post();

        // looping

    endwhile;
}
else {
    // no posts
}

fire_PHP shows the WP_Query contains:

WP_Query(
    query =
    array(
        ['posts_per_page'] => 4
        ['post_type'] =>
            array(
                [0] => 'page'
                [1] => 'post'
            )
        ['category__in'] => 22
    )

    query_vars =
    array(

        ['posts_per_page'] => 4
        ['post_type'] =>
            array(
                [0] => 'wod'
                [1] => 'attachment'
            )
        ['category__in'] =>
            array(
                [0] => 22
            ) 

I'm using Genesis, building upon the sample child theme. In order to display my custom post type 'WOD' on the homepage I've added to functions.php:

add_filter( 'pre_get_posts', 'ml_wods_home' );
function ml_wods_home( $query ) {

    if ( is_home() && false == $query->query_vars['suppress_filters'] )
        $query->set( 'post_type', array( 'wod', 'attachment' ) );

    return $query;
}

as suggested at Justin Tadlock. This works well on its own, but seems to be interfering with the slider query:

Please note that query['post_type'] is overwritten by query_vars['post_type']. In addition, the browser is timing out frequently as I mess around...

I found that Michael Fields had a related issue , posted a solution, (which I think I'm implementing) but its not working for me, and I don't understand it..

What am I missing?! TIA, Tim

2
  • I guess your condition fails... Mar 12, 2012 at 5:43
  • Hi Rutwick, are you referring to the condition in ml_wods_home()? this runs fine, but I think it interferes with the feature-slider.. i'll clarify the question!
    – ptim
    Mar 12, 2012 at 7:45

1 Answer 1

4

First of all,while( have_posts()): should be while( $ml_featured->have_posts()):

And secondly, in your pre_get_posts callback you check:

false == $query->query_vars['suppress_filters']

But this returns true for every WP_Query query and any get_posts query where 'supress_filters' is explicitly set to false. If you want to check if the query is the 'main' query (i.e. query associated with the global $wp_query), you can use:

$query->is_main_query();

(see documentation here). This returns true if $query is the main query and false otherwise. This function was introduced in 3.3. Before 3.3 you can probably check (untested):

global $wp_the_query;
if($query === $wp_the_query){
  //is main query
}else{
  // is not main query
}

Alternatively (but preferably use the methods above), to alter the main query only:

 global $wp_query;
 $wp_query->set( 'post_type', array( 'wod', 'attachment' ) );
1
  • oh, man - all the options - thanks a million I was going mad!
    – ptim
    Mar 12, 2012 at 23:25

Your Answer

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

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