Add the filter where you need it and remove it afterwards.
add_action('pre_get_posts','blog_filter');
// your query; presumably WP_Query
// your first loop
remove_action('pre_get_posts','blog_filter');
// another query; presumably WP_Query
// your second loop
You can also make your action self-removing:
function blog_filter($query) {
remove_action('pre_get_posts','blog_filter');
if ( !is_admin() && $query->is_main_query() ) {
if ($query->is_post_type_archive('rg_blog')) {
$query->set('post_type', 'post');
}
}
}
add_action('pre_get_posts','blog_filter');
Your code, reorganized:
// define the function; can be here or functions.php
function blog_filter($query) {
if ( !is_admin() && $query->is_main_query()) {
if ($query->is_post_type_archive('rg_blog')) {
$query->set('post_type', 'post');
}
}
}
get_header();
// blog title and description query
$post_id = 4606;
// add the filter
add_action('pre_get_posts','blog_filter');
$queried_post = get_post($post_id);
// remove the filter; the query is done
remove_action('pre_get_posts','blog_filter');
$title = $queried_post->post_title; ?>
<h1><?php echo $title;?></h1><?php
// Main Loop (All Posts)
$post = $posts[0];
$c=0;
if (have_posts()) {
while (have_posts()) {
the_post();
// Featured Post
$c++;
if( !$paged && $c == 1) {
// stuff
// Other Posts
} else {
// the remaining posts
}
}
}
// The end of the loop
// third loop, a repeat of loop 1
// blog title and description query
$post_id = 4606;
$queried_post = get_post($post_id);
$title = $queried_post->post_title; ?>
<h1><?php echo $title;?></h1><?php
echo $queried_post->post_content;
get_footer();
I have not tested that but I think it will work better. Hopefully, I don’t have any syntax errors.
Edit:
I took a closer look at your code (while not watching “Defiance”) and notices a couple of things.
- Your filter will only effect the main query so you will need to add
it before the page template loads– probably infunctions.php. But
my understanding of the question is that the filter is meant for a
secondary Loop. That would mean that that “main query” check is
wrong. - You are requesting posts via
get_postby ID, so for those the
filter is irrelevant.
So, to filter that main query you need this in functions.php:
// define the function; can be here or functions.php
function blog_filter($query) {
// remove the filter; the query is done
remove_action('pre_get_posts','blog_filter');
if ( !is_admin() && $query->is_main_query()) {
if ($query->is_post_type_archive('rg_blog')) {
$query->set('post_type', 'book');
}
}
}
add_action('pre_get_posts','blog_filter');
And no add or remove actions anywhere else. I am not sure that is what you want though. The two get_post calls should be unaffected.