0

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.

3 Answers 3

1

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

5
  • 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
    Commented Jan 17, 2014 at 14:18
  • You are right, i updated my answer with an alternative. Please check it, it should be working fine.
    – Shazzad
    Commented Jan 17, 2014 at 17:31
  • Thanks you, but it's doesn't work. :(
    – Saliom
    Commented Jan 17, 2014 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
    Commented Jan 17, 2014 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
    Commented Jan 17, 2014 at 19:10
0

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

3
  • 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
    Commented Jan 17, 2014 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? Commented Jan 17, 2014 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
    Commented Jan 17, 2014 at 12:00
0

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.

2
  • it would take too much space on the page of my article I select all categories (more than 5 lines).
    – Saliom
    Commented Jan 17, 2014 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
    Commented Jan 17, 2014 at 12:08

Your Answer

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

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