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.

How can I apply pre_get_posts to a custom query?

For example, if I have:

$custom_query = new WP_Query(...)
//code here

How can I apply the pre_get_posts for this $custom_query?

share|improve this question
add comment

2 Answers

For some use cases, couldn't you use $query->get() to target specific custom queries?

For example: say you've got a custom query for a slider post-type. Perhaps something like this?

function wpse64950_filter_pre_get_posts( $query ) {
    if ( ! is_main_query() ) {
        if ( 'slider' == $query->get( 'post-type' ) ) {
            // This is a query of slider posts,
            // and isn't the main query;
            // Do something
        }
    }
    return;
}
add_filter( 'pre_get_posts', 'wpse64950_filter_pre_get_posts' );

Granted, this will target every such slider post-type custom query; so it won't work in every case. But it should work in some cases.

share|improve this answer
add comment

pre_get_posts fires for every post query.

There is really no reason to apply it to a specific custom query. If you don't want it to be applied to your custom query you need to make sure you are checking for is_main_query().

share|improve this answer
    
I want it to be applied to a custom query, but not on the main query. Here's what i'm trying to achieve - i want to list posts with closed comments on one page, and i think it can be achieved with comment_status, 'open', using the pre_get_posts. On the other hand, i want to do that with just one custom query, while the main query should remain intact. Or is my logic wrong, and i'm missing something? –  Nicholas Sep 12 '12 at 17:12
1  
Why don't you just pass the necessary parameters to that specific query, since you're setting it up anyway? –  Pippin Sep 12 '12 at 17:36
    
@Pippin - how would i do that in my case? I want the main parameter to be are comments open or not? so i want to create a query that will show only posts with closed or opened comments? How would i do that? Any help would be appreciated. –  Nicholas Sep 12 '12 at 19:03
    
You would do it in exactly the same way that you would with pre_get_posts. Do you know how to do it there (excluding the part about targeting a specific query)? –  Pippin Sep 12 '12 at 22:06
    
@Pippin Would it look something like this? function comm_cl( $query ) { $query->set( 'comment_status', 'open' ); } I had this bit of code, gist.github.com/3709991 - it is working, but it's not working well with pagination. So that's really the problem i'm trying to solve, and i was hoping i can achieve it with pre_get_posts. Any help on the matter would be appreciated. –  Nicholas Sep 12 '12 at 22:32
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.