Category: Advanced Custom Fields

Multisite: Copy a Page From One Site to Another


I’ve been working on a multisite recently and my client requested something off the cuff that made me gulp. They said “I take it we will be able to copy a page from one site to the other to save us having to replicate it manually?”

Coping a page on the same site is pretty straightforward and there are a multitude of plugins out there that make it a pretty easy process. However. Doing this across a mulitsite is different. Not a lot of plugins out there.

wpmaz says*For this to work full with advanced custom fields you need to ensure your custom fields setup on both sites are the same!
I thought I would share a function I wrote today to achieve this. This is pretty basic at the moment but gives the core functionality and works with Advanced Custom Fields too*!

function duplicate_over_multisite($post_id_to_copy, $new_blog_id) {

    $post = get_post($post_id_to_copy);

    $title =  get_the_title($post);
    $post = array(

    	    // The new page title
            'post_title' => "Copy of" . $title,
            // Set the status of the new page
            'post_status' => 'draft',
            // The post type of this page
            'post_type' => 'page',
            // The user ID you want to assign to this page creation
            'post_author' => 1

    $data = get_post_custom($post);

    $meta_values = get_post_meta($post_id_to_copy);


    	$post_id = wp_insert_post( $post );
        foreach ( $data as $key => $values) {
            foreach ($values as $value) {
                add_post_meta( $post_id, $key, $value );

  	foreach ($meta_values as $key => $values) {
            foreach ($values as $value) {
                add_post_meta( $post_id, $key, $value );

     return $post_id;


I’m pretty happy with this, but think I might make a wee plugin for it!


ACF Option Fields on a Multisite

Get Options Fields From Advanced Custom Fields on a WordPress Multisite Installation


I’ve recently been working on a multisite project where it became apparent that it would be nice to have a ‘global options’ page. This would hold values to be used over all other sites on that same WordPress multisite network. At the time of writing this post there is no way to achieve this with functions built into Advanced Custom Fields. So I wrote my own.


wpmaz says'wp_' is the default database prefix added when installing WordPress. This can different if it pleases you.
In Advanced Custom Fields when an Option Field is saved by a user in the WordPress CMS it is saved into the ‘wp_options’ table of the database.

Like so:

option_name option_value
options_date_of_event 20150410

The ‘option name’ can be broken up into two sections. First ‘options_’ which is the prefix for an Advanced Custom Field option key and then then name of the field which is, in the above example, ‘date_of_event’. We could create a WordPress function to get this option value by querying the database:

global $wpdb;

$query =   "select option_value
	    from ". $wpdb->base_prefix . "_options
	    where option_name = 'options_date_of_event'";

$result = $wpdb->get_var($query);

return $result;

Note that the above example is redundant because you can already do the same thing much easier with:


However! The reason we are talking about this is if we wanted to get a value from another site on the same WordPress multisite installation!

To do this we need to look at how WordPress manages its multisite database.

In a normal installation we may see a list of tables like so:

wp_commentmeta, wp_comments, wp_links, wp_options, wp_postmeta, wp_posts, wp_terms, wp_term_relationships, wp_term_taxonomy, wp_usermeta, wp_users.

With each site that is added WordPress will increment the names of the tables in the database like this:

wp_2_commentmeta, wp_2_comments, wp_2_links, wp_2_options, wp_2_postmeta, wp_2_posts, wp_2_terms, wp_2_term_relationships, wp_2_term_taxonomy, wp_2_usermeta, wp_2_users.

Where the ‘2’ is actually the site or ‘blog’ ID number.

We can therefore adjust the query to be dynamic enough so we can point the query to the correct options table for a desired sub site on the multisite network.

The Function

Add this to your functions.php file:

function wpmaz_get_multisite_option($blogid, $optionName){

	 global $wpdb;
         $blogid = ($blogid == 1) ? '' : $blogid . '_';                 

         $query = "select option_value
                   from ". $wpdb->base_prefix . $blogid . "options
                   where option_name = 'options_". $optionName ."'";
         $result = $wpdb->get_var($query);

         return $result;

You can now call the function in your template file(s) like:

<?php echo wpmaz_get_multisite_option('1', 'date_of_event') ?>

Where ‘1’ is the ID for the site holding the Global options, or the site options you wish to use and ‘date_of_event’ is the ‘Advanced Custom Field’ field name.

Notes on Final Code

Line 5: This is an if statement in php shorthand to provide logic to get round the fact that the first table in the database (ID = 1) doesn’t have a numerical prefix.

Line 8: I used $wpdb->base_prefix incase the normal WordPress database prefix wasn’t used in your WordPress multisite installation.