WordPress.org

Make WordPress Core

Opened 22 months ago

Last modified 20 months ago

#21219 new defect (bug)

get_post_type_archive_link in multisite context: struct issue

Reported by: michelwppi Owned by:
Milestone: Awaiting Review Priority: normal
Severity: normal Version:
Component: Posts, Post Types Keywords:
Focuses: Cc:

Description

Context : multisite and a site aaa ( http://localhost/wpmu_svn_34/aaa/ ) trying to display the post_type_archive_link of cpt 'book' of the blog bbb

switch_to_blog( $b );
$link = get_post_type_archive_link( 'book' ) ;  
/*/ http://localhost/wpmu_svn_34/bbb/book/ and not  http://localhost/wpmu_svn_34/bbb/livre/ */
restore_current_blog();

when name, slug and query_var of the post_type are the same: no problem because $struct to find the '/book/' is kept from the caller site BUT

if in the called site, the CPT have the same type name ('book') but with different slug and query_var, the home_url is correctly changed but the $struct is not good... 'book' instead 'livre' as declared in cpt in site bob.

The issue came from global $wp_rewrite; which is not this of the target after the switch_to_blog !

Instead of a non elegant filter post_type_archive_link, is there a way to "find" the $wp_rewrite of $b ?

Best regards !

Change History (5)

comment:1 michelwppi22 months ago

  • Cc contact@… added

comment:2 michelwppi22 months ago

Global $wp_post_types seems to be also concerned through get_post_type_object

comment:3 follow-up: michelwppi22 months ago

This issue is the same in get_post_permalink function (called by get_blog_permalink via get_permalink) which uses the global $wp_rewrite of the caller site and not this of the target site. today the workaround is to use the filter post_type_link.

comment:4 in reply to: ↑ 3 pathawks20 months ago

Replying to michelwppi:

today the workaround is to use the filter post_type_link.

I'm having this same issue.
Can you explain a bit more about how to use post_type_link?

comment:5 michelwppi20 months ago

Thanks for your question.. in this code lines, you will find how to do

add_filter ('post_type_archive_link', 'switch_query_book', 10, 2);
add_filter ('post_type_link', 'switch_query_book', 10, 2);

function switch_query_book ( $link, $post_type) {
	global $blog_id;
	$link .= '#bid='.$blog_id; // here for test - comment if ok
	switch ( $blog_id ) {
		case 1 :
		$search = "livre";
		$replace = "book";
		break;
		
		case 2 :
		$search = "book";
		$replace = "livre";
		break;
		
	} 
	
	$link = str_replace ($search, $replace, $link);
	
	return $link;	
}

Enjoy awaiting a better WP architecture with more separation !

Note: See TracTickets for help on using tickets.