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 altered the following code from Ashfame:

add_action('pre_get_posts', 'block_cat_query' );
function block_cat_query() {
global $wp_query;   
if( is_home() || is_search() || is_tag() ) {
    $wp_query->query_vars['cat'] = '3';
}
}

This code DOES work to filter posts from category #3 only on the home, search and tag pages.

However, I am utilizing

<?php single_tag_title(); ?>

in my header.php file and on specific tag pages, the tag title is not showing up. The page is instead showing the category title. This is only happening with this function in place, so I'm trying to figure out if there is another way of writing the function above that does not effect the use of <?php single_tag_title(); ?> on tag pages.

share|improve this question
add comment

3 Answers

up vote 0 down vote accepted

If the only issue you want to correct is the output of single_tag_title, you can correct the value via a filter by grabbing the tag query var directly:

function wpa90852_fix_tag_title(){
    $tag = get_term_by( 'slug', get_query_var('tag'), 'post_tag' );
    return $tag->name;
}
add_filter( 'single_tag_title', 'wpa90852_fix_tag_title' );
share|improve this answer
    
Thanks @Milo - that totally worked!!! –  Beth Blinebury Mar 14 '13 at 18:30
add comment

First you have to find the right hook, here it is pre_get_posts. Then you have to check if you are on the front page (is_front_page()) and if you are in the main query (is_main_query). As the $query is given as a parameter it's easy to alter the query.

/**
 * WordPress Answers #90852
 *
 * @param obj $query WP_Query object
 * @return void
 * @author Ralf Hortt
 **/
function pre_get_posts_90873( $query )
{
    if ( is_front_page() && $query->is_main_query() && !is_admin() ) :
        $query->query_vars['cat'] = 3;
    endif;
}
add_action('pre_get_posts', 'pre_get_posts_90873' );

PS: Ok, I'll see the question has changed. I still let my answer her, just in case someone want to know.

share|improve this answer
    
Sorry Horttcore! Your code does work but I need it to filter on the tag pages as well. And my main issue is the use of the single tag title code. –  Beth Blinebury Mar 14 '13 at 17:23
    
Well it is not hard to alter the if clause to work on the tag page as well. Whats your problem with the single tag title? –  Horttcore Mar 19 '13 at 13:04
add comment

There're a lot of methods for the WP_Query that you can use to achieve your goal. If you want to block a category on:

  • The home page
  • The Search-results page
  • On tag pages

by its ID, then you can use category__not_in.

add_filter( 'pre_get_posts', 'wpse90852_block_cat_query' );
function wpse90852_block_cat_query( $query )
{
    if ( is_admin() )
        return $query;

    if ( $query->is_main_query() && ( $query->is_home() || $query->is_search() || $query->is_tag() ) )
    {
        $query->set( 'category__in', 3 );
    }

    return $query;
}
share|improve this answer
    
Sorry, let me be more clear... I only want to SHOW category #3. And the function I originally posted does work, however tag pages are showing up as category pages. Does that make sense? For example, if I am looking at a specific tag page while logged in, the WP-admin bar shows "edit category" instead of "edit tag." And the tag name does not show up, but the category name. I'm just trying to figure out why the tag pages are showing up like they're category #3 pages. –  Beth Blinebury Mar 14 '13 at 15:48
    
@BethBlinebury What exactly are you trying to do: A) Public/Theme modification or B) modifiying the admin UI? If it's about themes, then see the update. If you're trying to query for a category with the ID of 3 as well on every of those listed pages, then you might want to go with category__and. –  kaiser Mar 14 '13 at 16:02
    
I'm trying to modify my theme. I tried the updated function you posted and it's throwing an error for 'set' –  Beth Blinebury Mar 14 '13 at 16:07
    
The original function I posted DOES work. It's just that I'm using <?php single_tag_title(); ?> in my header.php file and with that function, the single tag title is showing up as the category title. That's really the only issue I'm trying to resolve but I thought maybe a different way of writing it would do the trick. –  Beth Blinebury Mar 14 '13 at 16:10
    
@BethBlinebury Then please update your question with those very specific details... And if you say "error", then please as well tell the exact error description. Error is pretty vague by itself. –  kaiser Mar 14 '13 at 16:30
show 7 more comments

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.