The MU forums have moved to WordPress.org

To get permalink for posts! (15 posts)

  1. skcsknathan001
    Member
    Posted 18 years ago #

    Finally I found out how to get permalink for posts outside themes index page.

    there is a function called get_blog_permalink( $blog_id, $post_id )

    Just pass the blog id and post id, and it will return the permalink.

    that function is found in \wp-includes\wpmu-functions.php

  2. demonicume
    Member
    Posted 17 years ago #

    ok, for us non php'ers - how do i 'pass the blog id and post id'. i just followed a trail of like 7 posts to get here... is this a hack for the 'recent posts' plugin by andrea?

  3. d_kc
    Member
    Posted 16 years ago #

    Anybody care to elaborate on this ?

    I'm also asking how this can be implemented on the recent posts plugin originally by andrea(& hubby :P) so we can use the real permalinks instead of the guid.
    http://wpmudevorg.wordpress.com/project/Most-recent-posts

  4. d_kc
    Member
    Posted 16 years ago #

    Anyone ?

    Looking to also fix this for MuTags

  5. MrBrian
    Member
    Posted 16 years ago #

    Not much to elaborate on.... this is what is used for the recent posts function.

    get_blog_permalink($blog, $thispost[0]->ID)

    and you have to make sure the query that populates $thispost is changed from guid to ID. Case sensitive too.

  6. d_kc
    Member
    Posted 16 years ago #

    As soon as i configure ah_recent_posts that way, the output comes out blank.. nothing is displayed.

    here's the code with get_blog_permalink.

    function ah_recent_posts_mu($how_many = 10) {
    	global $wpdb;
    	$counter = 0;
    	// get a list of blogs in order of most recent update
    	$blogs = $wpdb->get_col("SELECT blog_id FROM $wpdb->blogs WHERE
    		public = '1' AND archived = '0' AND mature = '0' AND spam = '0' AND deleted = '0' AND last_updated >= DATE_SUB(CURRENT_DATE(), INTERVAL 5 DAY)
    		ORDER BY last_updated DESC");
    
    	if ($blogs) {
    		foreach ($blogs as $blog) {
    			// we need _posts and _options tables for this to work
    			$blogOptionsTable = "wp_".$blog."_options";
    		    	$blogPostsTable = "wp_".$blog."_posts";
    
    			$options = $wpdb->get_results("SELECT option_value FROM
    										   $blogOptionsTable WHERE option_name IN ('siteurl','blogname')
    										   ORDER BY option_id, option_name DESC");
    		       // we fetch the title and link for the latest post
    		$thispost = $wpdb->get_results("SELECT ID, post_title, post_content, user_email
    											FROM $blogPostsTable, wp_users
    											WHERE wp_users.ID = $blogPostsTable.post_author
    											AND post_status = 'publish' AND post_type = 'post' AND post_date >= DATE_SUB(CURRENT_DATE(), INTERVAL 5 DAY)
    											ORDER BY $blogPostsTable.id DESC limit 0,3");
    			// if it is found put it to the output
    			if($thispost) {
    			// pull in the posts content from database
    			$desc = $thispost[0]->post_content;
    			// save the post date to a var
    			$date = strtotime($thispost[0]->post_date);
    			// strip out html characters to allow for truncating
    			$strippedDesc = strip_tags($desc);
    			$permalink = get_blog_permalink( $blog, $thispost[0]->ID );
    			// get authors gravatar
    			$avatar = get_avatar( $thispost[0]->user_email , 48 );
    			// truncate post content to 12 words
    			$numwords = 12;
     			preg_match("/([\S]+\s*){0,$numwords}/", $strippedDesc, $regs);
    		    $shortDesc = trim($regs[0]);
    				// Displays the post title url truncted text and soon author
    				// Author currently returns the ID (number) I will have to compare this to display_name and return a string name...
    				echo
    				'<li>
     				'.$avatar.'
    				<h2><a href="'.$permalink.'">
    				'.$thispost[0]->post_title.'</a></h2>
    				'.$shortDesc.'
    				<a href="'.$permalink.'">[...]</a>
    				</li>';
    				$counter++;
    			}
    			// don't go over the limit
    			if($counter >= $how_many) {
    				break;
    			}
    		}
    	}
    }
    ?>
  7. MrBrian
    Member
    Posted 16 years ago #

    Not sure, it looks fine actually.

  8. MrBrian
    Member
    Posted 16 years ago #

    I tried to get the permalink displaying on mutag's tag results page, but i think there's a bug with get_blog_permalink maybe - it's getting the permalink of the current displayed page no matter what blog_id i push to it. I will debug around ;)

    Here is my working recent posts function, but it's not exactly the same...

    function recent_posts_mu( $how_many = 10 )
    {
        global $wpdb;
        $counter = 0;
        // get a list of blogs in order of most recent update
        $blogs = $wpdb->get_col( "SELECT blog_id FROM $wpdb->blogs
    		ORDER BY last_updated DESC LIMIT 10" );
    
        if ( $blogs )
        {
            foreach ( $blogs as $blog )
            {
    			if( $blog != 1 ) {
                // we need _posts and _options tables for this to work
                $blogOptionsTable = "wp_" . $blog . "_options";
                $blogPostsTable = "wp_" . $blog . "_posts";
                $options = $wpdb->get_results( "SELECT option_value FROM
    				$blogOptionsTable WHERE option_name IN ('siteurl','blogname')
    				ORDER BY option_name DESC" );
                // we fetch the title and link for the latest post
                $thispost = $wpdb->get_results( "SELECT post_title, ID
    				FROM $blogPostsTable WHERE post_status = 'publish'
    				AND post_type = 'post'
    				ORDER BY id DESC LIMIT 0,1" );
                // if it is found put it to the output
                if ( $thispost && $thispost[0]->post_title != "Hello world!" )
                {
    //                echo '<li class="page_item"><a href="'.$thispost[0]->guid
    //                . '">' . $thispost[0]->post_title . '</a> by <a href="' . $options[0]->
    //                    option_value . '">' . $options[1]->option_value . "</a></li>";
    
                    echo '<div style=" background-color:#e9f5fd; clear:left; margin-top:10px; padding-top:7px; padding-bottom:7px;width:279px;"> <a href="' .
                        $options[0]->option_value . 'feed/"><img src="'. get_bloginfo('stylesheet_directory') . '/images/index_bg_10.jpg" style="float:right; margin-right:10px;" border="0"></a>
    						<a href="' . get_blog_permalink($blog, $thispost[0]->ID) .
                        '" style="font-size:13px; font-weight:bold; color:#194680; margin-left:10px; clear:left;text-decoration:none">' .
                        substr($thispost[0]->post_title, 0, 60) . '</a>
    
    						<p style="font-size:11px; font-weight:bold; color:#333333;margin-left:10px;"><a href="' .
                        $options[0]->option_value . '">' . $options[1]->option_value . '</a>
    
    					</div>';
    
                    $counter++;
                }
                // don't go over the limit
                if ( $counter >= $how_many )
                {
                    break;
                }
                }
            }
        }
    }
  9. MrBrian
    Member
    Posted 16 years ago #

    Nevermind, there's no bug :). I guess it's just too early for me to be coding hehe.

    In your tag_results.php template, change the_guid to
    echo get_blog_permalink($post->blog_id, get_the_ID());

  10. d_kc
    Member
    Posted 16 years ago #

    ughhh..

    I used your code as you posted it and it's showing mainblog.com/2008/08/08/post-name instead of subblog.mainblog.com/2008/08/08/post

    Same thing with second code when I put it in tag_results for MuTags.

    I'm using 2.6 RC1 for this test setup ..what are you using ?

  11. MrBrian
    Member
    Posted 16 years ago #

    edit: nevermind.

    Are you calling these functions from your main blog or no? Not sure why it's giving you the mainblog.com, but you could just get the proper domain with get_blog_option($blog_id, 'siteurl') and use str_replace to switch out the mainblog.com with subblog.mainblog.com. I run off of svn trunk, so were running pretty much the same wpmu version.

  12. andrea_r
    Moderator
    Posted 16 years ago #

    d_kc - every piece of code you're trying winds up with the same results. I'm betting money on something being wonky on your setup. :-/

  13. d_kc
    Member
    Posted 16 years ago #

    Andrea, just did a fresh install of the lastest MU (2.6.1) from trunk on a NEW server.

    And I'm still getting this. Something's wrong with my code. But Mr. Brian's code comes out blank for me on this new install as well.

  14. MrBrian
    Member
    Posted 16 years ago #

    Well, the code works, so you've got to be using it improperly or put it together wrong. Try pasting your full code or whatever at pastebin.com and telling us the full url of the page when you test it. You said before the only problem was that it didn't have the subdomain attached, now it's blank?

  15. andrea_r
    Moderator
    Posted 16 years ago #

    And I've done fresh installs with the same code, on new setups, and haven't had the same results. :)

    That's why I'm suggesting it's something to do with your setup / server / way you're holding your mouth, rather than something wrong with MU or any of the plugins. :)

    (she says, in a friendly way)

    And I don't know what could be interfering. Not without combing the server.

About this Topic

  • Started 18 years ago by skcsknathan001
  • Latest reply from andrea_r