NB: Make sure you do a back up of your theme, files and database before attempting the tutorials

Créer une requête dans un multisite WordPress
Intermediate
Web Hosting Canada

Last modified : Jul 07 2021

Estimated reading time : 2 minutes, 13 seconds - 238 words

In this tutorial we will show how to create a query that will pull a specific website’s post type within the multisite network.

In order to attain this, we will use the switch_to_blog(); function and find the blog ID that matches the website’s ID where we want to pull the post type data from.

Dans notre exemple ci-dessous, nous allons renvoyer nos résultats dans un shortcode du nom de [listeentreprises]. Ce shortcode affichera les résultats par exemple dans une page d’un autre site web du multisite. In the example below, we will generate our results with a shortcode titled [listeentreprises]. This shortcode will display the results on a page of another website from the multisite network.

We need to create our switch_to_blog(3) and close it with restore_current_blog which will create the multisite query. The ID 3 is our website’s ID.

////////////////////////////////////////////// Shortcode Afficher entreprises /////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////

add_shortcode('listeentreprises', 'themespress_afficher_entreprise');
function themespress_afficher_entreprise() {
    // Requete de l'ID de notre site web
    switch_to_blog(3);

    // préparer la query pour récupérer le post type à afficher
    $args = array(
        'post_type' => 'votreposttype',
	'posts_per_page' => -1,
	'order' => 'ASC',
        'orderby' => 'title',
    );
    $the_query = new WP_Query( $args );

    echo'<ul>';
	
	  while ( $the_query->have_posts() ) { $the_query->the_post();
		echo '<li>' . get_the_title() . '</li>';
         }
    echo'</ul>';

    // revenir à Local d’abord
    restore_current_blog();
    return $entreprise;
}

Afficher une vue formidable form dans un multisite WordPress

We have now created our results which included entreprise Post Type.

Following the same logic, we will create a shortcode and a return shortcode with Vue Formidable Form.

Our shortcode in the example below will be [vuespost]. This shortcode will display the results in a page of another website within the multisite network

Replace it by yours.

//////////////////////////////// Afficher vue formidable form  ///////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////

add_shortcode('vuespost', 'themespress_vuesPOST_shortcode');
function themespress_vuesPOST_shortcode(){
	// Requete de l'ID de notre site web
        switch_to_blog(3);
	$output = do_shortcode('[display-frm-data id=25033]');
	restore_current_blog();
	return $output;
}

NB : Make sure to do a backup of your theme and dabatase before using these functions.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes:

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>