Title: Linking posts by code
Last modified: September 6, 2019

---

# Linking posts by code

 *  [PG](https://wordpress.org/support/users/parulgarg2/)
 * (@parulgarg2)
 * [6 years, 9 months ago](https://wordpress.org/support/topic/linking-posts-by-code/)
 * Hi
 * Thanks for a wonderful plugin you have made!
 * I am currently trying to switch my single site to multisite. I have a number 
   of posts that I would like to link automatically through code. Is there any way
   to do that ?
 * Also, I have an input to make. Since I am having an author based website, I don’t
   want them to be able to link their posts from other authors.. so in MslsMetaBox.
   php may be you can try and integrate ‘author’ as well in the query. Right now
   I have manually handled it, but afraid to have to do it every time there is an
   update.
 * Thanks alot.

Viewing 2 replies - 1 through 2 (of 2 total)

 *  Plugin Author [Dennis Ploetner](https://wordpress.org/support/users/realloc/)
 * (@realloc)
 * [6 years, 9 months ago](https://wordpress.org/support/topic/linking-posts-by-code/#post-11905932)
 * Hello,
 * thanks for your message.
 * Is there any pattern that could be used to decide which posts should be tied 
   together?
 * For the request: Would you share some code to get the idea better?
 * Cheers,
    Dennis
 *  Thread Starter [PG](https://wordpress.org/support/users/parulgarg2/)
 * (@parulgarg2)
 * [6 years, 9 months ago](https://wordpress.org/support/topic/linking-posts-by-code/#post-11912775)
 * Hi
 * Thanks for your reply.
 * Yes, actually I have a custom post type that supports custom taxonomy `country`.
   Now when I am trying to move the single site to multisite, i have made different
   countries as different sites. I will run a script that will copy posts from the
   base site to their appropriate country’s site. In case of multiple countries,
   I needed to link them together.
    So, I was thinking of having site ids and post
   ids in hand; if I can link those posts together.
 * For the request:
    This is what I have done in MslsMetaBox.php
 *     ```
       $query = array(
       	'post_type'      => $type,
       	'post_status'    => get_post_stati( [ 'internal' => '' ] ),
       	'orderby'        => 'title',
       	'order'          => 'ASC',
       	'posts_per_page' => - 1,
       	'fields'         => 'ids',
       	'author'         => ''
       );
       if ( is_admin() && !current_user_can('edit_others_posts')){
       	$query['author']=get_current_user_id();
       }
   
       $my_query = new \WP_Query($query);
       ```
   
    -  This reply was modified 6 years, 9 months ago by [PG](https://wordpress.org/support/users/parulgarg2/).

Viewing 2 replies - 1 through 2 (of 2 total)

The topic ‘Linking posts by code’ is closed to new replies.

 * ![](https://ps.w.org/multisite-language-switcher/assets/icon-256x256.png?rev=
   2793358)
 * [Multisite Language Switcher](https://wordpress.org/plugins/multisite-language-switcher/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/multisite-language-switcher/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/multisite-language-switcher/)
 * [Active Topics](https://wordpress.org/support/plugin/multisite-language-switcher/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/multisite-language-switcher/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/multisite-language-switcher/reviews/)

 * 2 replies
 * 2 participants
 * Last reply from: [PG](https://wordpress.org/support/users/parulgarg2/)
 * Last activity: [6 years, 9 months ago](https://wordpress.org/support/topic/linking-posts-by-code/#post-11912775)
 * Status: not resolved