WordPress.org

Ready to get started?Download WordPress

Forums

Multisite Language Switcher
[resolved] Get blog posts relations in PHP (6 posts)

  1. shove
    Member
    Posted 6 months ago #

    Hi there,

    I'd like to combine all posts from all of my network sites into one single list. I'm using your great MSLS plugin to create the relations between blog-posts in different languages. There is no problem to get all posts of each network site in my overall posts summary but now I'm suck to get these relations from your plugin.

    Here is some of my testing code where I try to get all blog post permalinks and the related permalink for the translated blog post.

    // get list of all blogs (network sites)
    $blogs = $wpdb->get_results( $wpdb->prepare(
    		"SELECT * FROM {$wpdb->blogs} WHERE  spam = '0'
    		AND deleted = '0' AND archived = '0'
    		ORDER BY registered DESC, 5", ARRAY_A ) );
    
    // save current blog id (reset later)
    $original_blog_id = get_current_blog_id();
    
    $args = array(
    	'numberposts'     => -1,
    	'post_type'       => 'post',
    	'post_status'     => 'publish' );
    
    // loop through blogs
    foreach ($blogs as $blog) {
    
    	// switch to blog
    	switch_to_blog( $blog->blog_id );
    
    	// get all posts
    	$posts = get_posts( $args );
    
    	// loop through blog posts
    	foreach($posts as $post) {
    
    		setup_postdata( $post );
    		$permalink = get_permalink( $post->ID );
    
    		$msls_option = MslsOptions::create( $post->ID );
    
    		foreach ( $mls_blogs->get_objects() as $msls_blog ) {
    			switch_to_blog( $msls_blog->userblog_id );
    			$msls_language = $msls_blog->get_language();
    			$msls_permalink = $msls_option->get_permalink( $msls_language ) ;
    			echo( $msls_language .' : '. $permalink .' / '. $msls_permalink .'<br>');
    		}
    		restore_current_blog();
    	}
    }
    
    switch_to_blog( $original_blog_id );

    I hope someone could help me with this or knows where I could get some help. Maybe I'm not the first one to try this but I didn't find anything helpful so far.

    Thanks a lot! Shove

    http://wordpress.org/plugins/multisite-language-switcher/

  2. realloc
    Member
    Plugin Author

    Posted 6 months ago #

  3. shove
    Member
    Posted 6 months ago #

    Hey Dennis,

    thanks for your fast reply. I already stumbled across this wiki page and it helped me understanding how your plugin works. One interesting thing I noticed : getting the permalinks for the related (translated) posts works well from within the specific post but not from within a page. For example I tried this hard-coded testing function in my functions.php.

    function my_test() {
    	// switch to german blog
        switch_to_blog( 3 );
    
    	// output the permalink for the german post with id 1
    	error_log( get_permalink(1) ); // german link
    	// test 1 from within a blog post outputs : http://localhost/wordpress/de/hallo-welt/
    	// test 2 from within a page outputs : http://localhost/wordpress/de/hallo-welt/
    
    	// get MSLS data for post with id 1
    	$mydata = MslsOptions::create( 1 );
    
    	// switch to us blog
    	switch_to_blog( 1 );
    
    	// output the permalink for the corresponding us post
    	error_log( $mydata->get_permalink('us') );
    	// test 1 from within a blog post outputs : http://localhost/wordpress/hello-world/
    	// test 2 from within a page outputs : http://localhost/wordpress/
    }

    When calling it from a blog post it works well and outputs both permalinks correctly. But called from within a page is outputs the base blog url instead of the permalink for the corresponding post.

  4. realloc
    Member
    Plugin Author

    Posted 6 months ago #

    Is this page the homepage? This could cause this behaviour because the create-methode is a factory which will return a MslsOptions-object instead of a MslsOptionsPost-object in this case.

    Look here: http://lloc.github.io/Multisite-Language-Switcher/source-class-MslsOptions.html#64

    So you could try:

    $mydata = new MslsOptionsPost( 2 );

    Cheers,
    Dennis.

  5. shove
    Member
    Posted 6 months ago #

    Hey Dennis,

    this works very well in a first test. I think I can work with that. Thanks a lot for your fast help! And thanks also for the great plugin!

  6. realloc
    Member
    Plugin Author

    Posted 6 months ago #

    Very good. You're welcome, Shove.

Reply

You must log in to post.

About this Plugin

About this Topic