I have a WordPress multisite with one main site and four sub-sites. In a PHP template on my main site, I would like to get the posts from one of the sub-sites and print the permalinks to the page. How is this done?
My code is below–when this code is executed, permalinks are indeed printed to the screen, but they are incorrect–the post name is correct, but the path is incorrect.
$tp_blog_id = 4;
switch_to_blog( $tp_blog_id );
$posts = get_posts(
array(
'post_type' => 'property',
'posts_per_page' => 100,
'numberposts' => -1
)
);
restore_current_blog();
error_log(print_r($posts,true));
foreach ($posts as $post) {
echo "The URL is: <br>";
echo get_blog_permalink( $tp_blog_id, $post->ID ) . "<br>";
}
Here is a screenshot of the output page. The permalinks incorrect–each of those leads to a 404 page. The incorrect permalinks printed in this case are of the form
http://dev.thailandproperty-hh.com/pattaya/blog/property/1-bedroom-condo-central-pattaya-10/
when in actuality the permalinks are
http://dev.thailandproperty-hh.com/pattaya/property/1-bedroom-condo-central-pattaya-10/.
As a test, to my error log, I printed the contents of the $posts
array. It does indeed contain all the desired post objects. In addition, each post object in the array has a property guid
, the value of which is the correct permalink (screenshot). I guess as a workaround I could simply echo this property to the page, instead of get_blog_permalink()
. But I’d like to understand why the latter is not returning the expected permalink.
UPDATE
I register my CPT using code below:
$slug = get_theme_mod( 'property_permalink' );
$slug = ( empty( $slug ) ) ? 'property' : $slug;
$args = array(
'labels' => $labels,
'public' => true,
'publicly_queryable' => true,
'show_ui' => true,
'show_in_menu' => true,
'query_var' => true,
'rewrite' => array( 'slug' => $slug ),
'capability_type' => 'post',
'has_archive' => true,
'hierarchical' => false,
'menu_position' => null,
'supports' => $supports,
'taxonomies' => array( 'property_type' )
);
register_post_type( 'property', $args );