Ask your WordPress questions! Pay money and get answers fast! (more info)

Multisite with a shared custom post type, retaining the site URL WordPress

  • SOLVED

Hello all,

<strong>I will add more money if the question gets harder and harder to answer :) </strong>
I've been trying to do exactly what's in this [[LINK href="http://wordpress.stackexchange.com/questions/101054/multisite-with-a-single-shared-custom-post-type-while-retaining-site-url/102095#102095"]]stack exchange thread[[/LINK]], so basically query posts from my main site from different post types and keep the same url in the single post and I'm having a little bit of trouble getting it going.
Quote from the thread to explain what I want to accomplish:
<blockquote>Small multisite setup, two very different sites, both need to access an event custom post type. I don't want to duplicate posts (there are some plugins that "broadcast" a post to the network).

switch_to_blog(1) will get me part way there, where blog 1 is the "master" blog that contains the event data, as far as archive-style pages that list all the events. But my concern is that the URL/permalink of the event, when viewed from blog 2 will point to blog 1, and following that permalink will then take the user to the other blog, confusing the user.

Is there a solution (possibly using rewrite) that would allow an event post at //blog1/events/event1 to appear as //blog2/events/event1 when viewed from blog 2?</blockquote>

I also want to grab posts from the same place because I have a "like" plugin a long with a social media bar and would like to keep the like and share counts the same across the whole network. This is why I'm not using something like the broadcast plugin.

I'm using this [[LINK href="http://pastebin.com/p9mt0GfU"]]query and post types [[/LINK]] on my sub site to get the posts from the main site. The post types are the exact same on the mainsite, but show_ui is true.

Basically what's happening is when the query post type is set to 'post' I keep the same url like I want, but I get a "page is not found" message <strong>Edit*</strong> when I click on the post from the loop and when I set the post to a custom post type I get redirected to the subsites homepage <strong>Edit*</strong> when I click on the post from the loop.

<blockquote>There are two options:

Register the CPT on blog 2 with 'show_ui' => FALSE. Hook on blog 1 into save_post and copy the data to blog 2.

Pro: You can search in those posts. Correct templates will be used automatically.
Con: Duplicated data are always a little bit … dirty.</blockquote>
I am currently not using the save_post function to save the posts from the mainsite to the sub sites. Do I actually have to use save_post to work, or is there a way to get this working without it like the second answer says?

<blockquote>It also means that you must have access to the "blog of record" in order to be able to enter and update the post data, unless you're also prepared to hook into the save_post action of all the other blogs and fool them into writing the data to the other blog's tables. You would then also need to fudge that post type's List Tables and edit-post request as well, which is daunting to say the least.</blockquote>

Is there something wrong with my query?

Or will I have to do a rewrite?
<blockquote>Register an endpoint with an URL scheme like the CPT on blog 1 (EP_ROOT).

Pro: No duplicated data.
Con: You have to implement the template logic manually to load the correct theme file. And search will not work.</blockquote>

*note I'm a amateur

I'm feeling pretty clueless as to what is going on, or what I could be doing wrong at this point.

<strong>*Update*</strong> I used the [[LINK href="http://pastebin.com/p9mt0GfU"]]query [[/LINK]] and set the post type to 'post' to fiddle around. Again I am using this query to query posts from blog 1 on blog 2, while trying to maintain the url from the site I clicked on that post from. I still had the "Hello World!" default posts on blog 1 and 2. I clicked on "Hello World!" from the [[LINK href="http://pastebin.com/p9mt0GfU"]]custom query [[/LINK]] loop on blog 2 and the post appeared with the url I wanted (so far I've got the url to appear correct but page not found on other posts) I ran a little bit of a test to see if it was pulling that information from blog 2 by commenting on the blog 2 hello world and clicked on the post from the custom query again and the comment was there in the single post.(meaning it's looking for the post in blog 2's table not blog 1) I don't know how this would work with custom post types as I am still trying to figure out why I get redirected every time.

Thank you very much for any help I receive! :D

Answers (3)

2014-07-14

timDesain Nanang answers:

try to add flush rewrite rules in init hook

put this code:
flush_rewrite_rules();
after:
register_post_type('videos',$args);


info: http://codex.wordpress.org/Function_Reference/register_post_type#Flushing_Rewrite_on_Activation


Chris comments:

Thank you for the reply tim,

It didn't work, I'm still getting redirected to my home page.


Chris comments:

Would I have to add that to the main sites function.php post types as well?


timDesain Nanang comments:

Blog1 => mainsite
Blog2 => subsite

to avoid ambiguous :
- try to delete all entries in post and CPT from Blog2
- add new some entries in post and CPT in Blog1
- create a index.php file, then put in Blog2's themes folder
- create a 404.php file, then put in Blog2's themes folder

<blockquote>
set to 'post' I keep the same url like I want, but I get a "page is not found"
</blockquote>
if the CPT (post) defined, but the post_id cannot found will be redirect to 404.php

<blockquote>
set to a custom post type I get redirected to the subsites homepage
</blockquote>
if the CPT not defined, will be redirect to index.php
if the CPT defined, but the post_id cannot found will be redirect to 404.php

note:
'post' is a part of post_type that bulitin with wordpress
'post' and CPT are using the same table



Chris comments:

Thank you for the help timDesian,

I copied the 404 and index.php from my parent theme to child theme.
Deleted the posts on Blog 2

CPT(post) = redirected to 404.php

CPT(videos) = was at first redirected to the index now it's being redirected to my front page again.

Updated [[LINK href="http://pastebin.com/p9mt0GfU"]]pastebin[[/LINK]] for hopefully less confusion.

Thank you so much for the help. I'm terribly sorry if I'm being confusing.



Chris comments:

I also added new posts on blog 1

2014-07-14

Bob answers:

can you give your website url?


Chris comments:

Sorry Bob, I should've mentioned that the site is locally hosted with xampp, using sub directories.

2014-07-15

Romel Apuya answers:

global $switched;
switch_to_blog(2);
$types='your-post-type'; //value of post type
$posts = get_posts('post_type=$types');
<ul>
<?php foreach($posts as $post) : setup_postdata($post);?>
<li>
<a href="<?php echo get_page_link($post->ID); ?>" title="<?php echo $post->post_title; ?>"><?php echo $post->post_title; ?></a>
</li>
<?php endforeach ; ?>
<?php restore_current_blog(); //switched back to main site ?>


Chris comments:

Thank you for the answer Romel.

I entered in the post type my post type and the blog I wanted to get from, but the code didn't work, none of the posts showed up. *note the query I wrote gets a loop of the posts from blog 1 onto blog 2 just fine, but when I click on one of the posts from the loop I get redirected to blog 2's home page. If I set the post type to 'posts' I get a page is not found.

I'm pretty sure I also tried a WP_query with switch to blog and if I remember correctly it didn't allow to keep the urls as explained in the [[LINK href="http://wordpress.stackexchange.com/questions/101054/multisite-with-a-single-shared-custom-post-type-while-retaining-site-url"]]thread I posted[[/LINK]]:
<blockquote>switch_to_blog(1) will get me part way there, where blog 1 is the "master" blog that contains the event data, as far as archive-style pages that list all the events. But my concern is that the URL/permalink of the event, when viewed from blog 2 will point to blog 1, and following that permalink will then take the user to the other blog, confusing the user.
</blockquote>
I'll give it another shot though, here's the exact code I placed in my template file:
<?php
global $switched;

switch_to_blog(1);

$types='videos'; //value of post type

$posts = get_posts('post_type=$types');
?>

<ul>

<?php foreach($posts as $post) : setup_postdata($post);?>

<li>

<a href="<?php echo get_page_link($post->ID); ?>" title="<?php echo $post->post_title; ?>"><?php echo $post->post_title; ?></a>

</li>

<?php endforeach ; ?>

<?php restore_current_blog(); //switched back to main site ?>


Thank you again for your support!