Support

Account

Home Forums General Issues Relationship field for multisite

Solved

Relationship field for multisite

  • Hi! Sorry if this is the wrong place for this – I posted on stackoverflow but got no bites…

    I have replicated the relationship field in order to try to make a field where you can choose a post from the master blog of a wordpress multi site.

    What I can’t work out is where to put the switch_to_blog(1) and the restore_current_blog() lines. I have tried to put them in the query posts function in my version of /core/fields/relationship.php but it still only showed me post types from the current blog in the field editor and only posts from the current blog when editing a post.

    I assume I need to create a new filter or action similar to acf/fields/relationship/result but I have no idea where they are…

    Any help would be awesome – thank you!

    P.S. If there is an easier way you can think of to allow users of several multi sites to select multiple posts from a custom post type held on the master site I am not precious about doing it this way : )

  • Hi @rsrc1147

    What line did you try placing the switch_to_blog(1) function?

    I would try placing it at the very start of query_posts on line 133.

  • Hi elliot

    Thanks very much for replying. I had it in later than that so have moved it up to line 133 but I’m still only getting the current blog. The relationship picker works fine apart from picking up the wrong blog…

    Thanks again!

  • Hi @rsrc1147

    Thanks for the feedback. Perhaps because the query_posts function is run as an AJAX call, the switch blogs function doesnt work?

    Can you google around for any help regarding AJAX functions and switching blogs?

    Thanks
    E

  • hello!

    Just incase anyone else was interested, I thought I would share where I got to.

    The problem wasn’t with the query_posts as such, although I can still only get it to work with the switch to blog low down just before the query.

    The problem was that the function wasn’t getting called in the first place. I had thought that changing the add_actions at the top of the file so that any wp ajax action going to the acf query post went to my query post function, it would work.

    What I have actually ended up doing is writing some js in my plugin which watches the wp ajax call and if it is from one of my fields, it redirects it to my query post function.

    I can’t imagine anyone would want to replicate this but if someone does I would be happy to share. I haven’t quite worked out how to pull the list of post types in yet though.

    I think it is great you answer these questions in the first place and I am a big fan of ACF and all the things yo’ve managed to do with it!

  • Hi @rsrc1147,

    I’d be interested in looking at your code. I need to pull in post relationships on a multisite install. Do you have the code on Github or another code repository?

    Thanks!

  • Hi jacobdubail

    I haven’t posted it anywhere but would be happy to share. However, I have since discovered that rather than using the relationship field, which involved using the complicated ajax hack, using the post field, possibly inside a repeater if you want to choose multiple is MUCH easier and can be done with a replica of the post field.Would you be more interested in that?

    The only problem with it over the relationship picker is post types are created at run time. Therefore, if you have custom post types you want to pick from, you will need to create a cache of the list of post types. I have added a hook to my save post to create a cache which sits in my plugin folder and would be very happy to help you do the same if you don’t know how.

    James

  • Thanks James!

    I’m definitely interested in the easier, more straightforward approach, especially if it is more upgrade proof. I’ll only be dealing with standard posts, but seeing how you handle custom posts would also be useful.

    How would you like to share the code? Public dropbox link? email?

    Cheers,
    Jacob

  • Hi James,

    Any chance I could check out your code? I’m finally getting around to building this integration and could really use a kickstart.

    Thanks,
    Jacob

  • One last attempt to get a look at your code. Could you at least let me know which filter you’re hooking into? Is it acf/fields/relationship/query?


    @elliot
    , any idea which filter I can use to do a switch_to_blog() just before the relationship query runs?

    Thanks,
    jacob

  • Hi @rsrc1147,

    Any chance I could see some of your code? I’m under the gun here and really need to get this up and running. Any help you can provide will be greatly appreciated.

    Thanks,
    Jacob

  • Hi,

    It’s a really old post but a solution with ACF 4 and a page link field is to create a new field type in a plugin or in functions.php of the theme. I suppose it can be done with a post field too. Here’s an example :

    
    class acf_field_page_link_multisite extends acf_field_page_link
    {
    
      function __construct()
      {
        // vars
        $this->name = 'post_object_multisite';
        $this->label = __("Page Link Multisite",'acf');
        $this->category = __("Relational",'acf');
        $this->defaults = array(
                           'post_type' => array('all'),
                           'multiple' => 0,
                           'allow_null' => 0,
    		      );
    
        // do not delete!
        acf_field::__construct();
      }
    
    	function create_field( $field )
    	{
              // Switch to the blog you want
    	  switch_to_blog(5);
    	  parent::create_field($field);
    	  restore_current_blog();
    	}
    
    	function create_options( $field )
    	{
    	  switch_to_blog(5);
    	  parent::create_options($field);
              restore_current_blog();
    	}
    
    	function format_value_for_api( $value, $post_id, $field )
    	{
    		switch_to_blog(5);
    		$value = parent::format_value_for_api($value, $post_id, $field);
    		restore_current_blog();
    		return $value;
    	}
    
    }
    
    new acf_field_page_link_multisite();
    
Viewing 13 posts - 1 through 13 (of 13 total)

The topic ‘Relationship field for multisite’ is closed to new replies.