Support

Account

Home Forums General Issues CPT that generates subdomains with fields from other CPT Reply To: CPT that generates subdomains with fields from other CPT

  • I’ve solved a similar problem through the use of a Multi site installation and using the switch_to_blog() function to call in what I need.

    http://codex.wordpress.org/Function_Reference/switch_to_blog

    In my solution I had a CPT that I wanted to bring into each of my subdirectories and then give the site user the option to select which posts they wanted to see on their site. I think something similar to this would work for you, although as I’m remembering I think I did have to hack about a little to make it work properly.

    Without the hacky-bit in the backend, I would say the easiest method is the following:

    – Setup your site as a subdirectory multisite. You’ll still need to manually create the subdirectory sites, but if you’re going to have to manually setup the CNAMES and stuff anyway, then this doesn’t end up being too much additional work.

    – Clock the ID of the subdirectory.

    – Add a new CPT property post on the core site, with the details of the property. Within this post, add a new field for “multisite” in which you put the SiteID.

    – Make a new page in the multisite, set it as the homepage.

    – Make a child theme for all multisites to use with a front-page.php template with something like the following

    <?php
    
    get_header()
    
    $original_site_id = get_current_blog_id();
    
    switch_to_blog(1); // 1 is the root blog with all the CPT posts in
    
    $query_args = array(
    	'post_type'	=>  'properties',
    	'meta_query' => array(
    		array(
    			'key'     => 'property_site_id',
    			'value'   => $original_site_id,
    		),
    	),
    );
    
    $query = new WP_Query($query_args);
    
    // Continue with standard WP loop and properties output
    
    restore_current_blog(); // Important to do this for other template requirements in the footer
    
    get_footer();
    
    ?>

    That should then give you the option to show a multisite exclusively using ACF fields from your primary website that you can edit everything in one place in one go.

    In practice you can also use this to give each user their own contact forms, newsletter signups etc. which may or may not be useful in the future.

    I would also say that if you decide to give multisite sites ACF fields they need to fill out, it’s much more practical to export those and put them into the child-theme as external files, as they will be auto-created on each site for you.

    Hopefully that helps? 🙂