1

I have a main website with 2 sub domains.

www.example.com

www.space.example.com

www.products.example.com

I want to publish articles on my main website using categories to sort them.

for example, an article using "space" category, and an article using "products" category.

Using wp-query and custom post type, I want to display on my space.example.com page the articles from "space" category, and on my products.example.com the articles from "products" category.

I know it's working using wp-querie with categorie on a simple website without sub domains :

$query = new WP_Query( 'category_name=space' ); 

or

$query = new WP_Query( 'category_name=products' );

but is there a way to do it with sub-domains ?

I can't find any solution on the net.

can anybody help me with this ?

thanks a lot

1

What you can do is to set up a pre_get_posts() filter in the subdomains' theme functions.php file to restrict posts to the desired category...

function my_subdomain_category( $query ) 
{
    if ( $query->is_main_query() ) 
    {
        $query->set( 'cat', '123' ); // use the categoryID for space or products
    }
}
add_action( 'pre_get_posts', 'my_subdomain_category' );
1

thanks @BA_Webimax

i've found the solution by using this query inside my subdomains pages to get datas from my main domain pages. main domain blog ID is (1). and sub domain for example are 2 and 3.

<?php switch_to_blog(1); ?>
<?php restore_current_blog(); ?>

look like this to display on a page from a sub domain, all the titles from posts with category "products" from my domain ID 1:

<?php

switch_to_blog(1); //define the domain containing the posts
$args = array('category_name' => 'products' , 'orderby' => 'title', 'order' => 'ASC' );

$myposts = get_posts( $args );
foreach ( $myposts as $post ) : setup_postdata( $post ); ?>

<?php the_title(); ?>

<?php endforeach;
restore_current_blog(); //switch back to the actual domain
wp_reset_postdata();?>

hope it will help ! very usefull when working with subdomains. it also works to get datas from custom fields and advanced custom fields...

best

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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