Skip to main content
Alternative & proper way to achieve that
Source Link
Shazzad
  • 3.3k
  • 1
  • 17
  • 23

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

That's it. You are all done

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

Source Link
Shazzad
  • 3.3k
  • 1
  • 17
  • 23

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' );

That's it. You are all done