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 wonder if it is possible to have the default category WordPress on all category archives?

Here is my situation: I created a website of video games and I renamed the default category "multiplatform". But I want all posts in this category are displayed on other categories like the category "PS3" for example.

If possible, could you help me to access this feature?

Edit: Here's a little example to explain it:

Currently:

  • The archive of the "Android" category displays only the items with the category "Android" assigned.
  • Assign a post to the "multiplatform" category will only appear on the archive section of posts and the archive of the category "multiplatform" page.

What I want:

  • Assign a post to the "multiplatform" category will appear on the archive section of the category "Android" as well as all other categories.
share|improve this question

3 Answers 3

up vote 1 down vote accepted

You need to use pre_get_posts hook. Where u can include the default category posts on other category pages.

function wpse_pre_get_posts( $q )
{
    // its the main query, and its the category page archive
    if( $q->is_main_query() && $q->is_category() ) :

        // get the default selected category id, fallback to 1 (default one id)
        $default_cat = (int) get_option('default_category', 1);

        // if the category page is not the default category page
        if( $default_cat != $q->get_queried_object_id() ):

            // get the already used tax_query arguments
            $tax_query = $q->get('tax_query');

            // add the default one
            $tax_query[] = array(
                'taxonomy' => 'category', 
                'field'    => 'term_id', 
                'terms'    => $default_cat 
            );

            // relation should be or, which means either the current category or the default
            $tax_query['relation'] = 'OR';

            // set the values to query
            $q->set('tax_query', $tax_query );

        endif;

    endif;
}
add_action( 'pre_get_posts', 'wpse_pre_get_posts' );

Ok, now this might conflict wp_query variable, so the next solution would be using post_request filer, which is used to filter the raw query.

function wpse_posts_request( $request, $q )
{
    global $wpdb;

    // its the main query, and its the category page archive
    if( $q->is_main_query() && $q->is_category() ) :

        // get the default selected category id, fallback to 1 (default one id)
        $default_cat = (int) get_option('default_category', 1);

        // category now
        $cat_now = $q->get('cat');

        // if the category page is not the default category page
        if( $default_cat != $cat_now ):

            $request = str_replace("$wpdb->term_relationships.term_taxonomy_id IN ($cat_now)", "$wpdb->term_relationships.term_taxonomy_id IN ($cat_now, $default_cat)", $request);

        endif;

    endif;

    return $request;
}
add_filter( 'posts_request', 'wpse_posts_request', 10, 2 );

That's it. You are all done

share|improve this answer
    
It's work... but now this hook replaces the value of the "single_cat_title" function as well as the title of the page and it will create a conflict. it would not be a means to ensure that the default category is not taken into account in all this? –  Saliom Jan 17 at 14:18
    
You are right, i updated my answer with an alternative. Please check it, it should be working fine. –  Shazzad Jan 17 at 17:31
    
Thanks you, but it's doesn't work. :( –  Saliom Jan 17 at 18:00
    
I did tested that on my local server on a clean installation. Can you pasted the output of the $request data from your site ? Put print_r($request); just above the return $request; line and it should display the raw codes –  Shazzad Jan 17 at 18:29
    
I tried but it doesn't display anything. I doubt that the code isn't executed on the category archive page... –  Saliom Jan 17 at 19:10

You can use the bulk edit feature to add a category to all posts in an existing category. Use the filter by category feature.

share|improve this answer
    
I think you did not understand me, I want to select the default category have the same result select all my categories (more than 30 ...) –  Saliom Jan 17 at 11:39
    
Why can't you filter all posts in your default category and then bulk edit them by assigning them to other categories simply by using the check box for another category? –  Brad Dalton Jan 17 at 11:54
    
I'm sorry if I expressed myself badly, I edited my first post. And I do not want to select my +30 categories because it would take too much space on the page of my article (more than 5 lines). –  Saliom Jan 17 at 12:00

You can assign your posts to multiple categories. Just check the checkbox before the categories you want your posts to be assigned in post editor page to and your posts will be shown in all the category archives you selected.

Just to let you know, when you assign your post to a category, it won't be automatically assigned to your default category unless you manually select that category.

share|improve this answer
    
it would take too much space on the page of my article I select all categories (more than 5 lines). –  Saliom Jan 17 at 12:03
    
I can't think of any other way to get around that. You can either remove the code that outputs the Categories in your posts pages, or use the comma separated list of categories there. –  Abhik Jan 17 at 12:08

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.