The MU forums have moved to WordPress.org

1 2 3

Featured Post (61 posts)

  1. DailyTestimony
    Member
    Posted 13 years ago #

    Well, this will be fun, I am working on a way to show the most recent post of one of my featured blogs on my site.

    So I've gotten this far,

    $featured_blogs = array(2,7,59,62);
    srand(time());
    $random = (rand()%4);
    $featured_blogs[$random];

    basically the idea is the array holds the blog id's of the featured blogs, a random number generator chooses one as the featured blog that will be shown on the page.

    What I need to do is get the most recent post from that blog, from my understanding their is a function that will do this if I also have the post ID, so is their an easy way to get the post ID of the most recent post?

    EDIT: Well I did find the function I was thinking of get_blog_post ( $blog_id, $post_id ), but the second issue is there are no useage instruction in codex for this function.

  2. MrBrian
    Member
    Posted 13 years ago #

    Pulled from recent posts plugin (http://atypicalhomeschool.net/wordpress-plugins/):

    //$blog is whatever blog you want to get the post of, so i made it your randomly generator featured blog
    $blog = $featured_blogs[$random];
    $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, guid
    FROM $blogPostsTable WHERE post_status = 'publish'
    AND post_type = 'post' AND post_date >= DATE_SUB(CURRENT_DATE(), INTERVAL 1 DAY)
    ORDER BY id DESC LIMIT 0,1");

  3. MrBrian
    Member
    Posted 13 years ago #

    Then an example of usage:

    if($thispost) {
    echo '<li class="page_item">guid
    .'">'.$thispost[0]->post_title.'
    by <a href="'
    .$options[0]->option_value.'">'
    .$options[1]->option_value."


  4. ";
    $counter++;
    }

  • DailyTestimony
    Member
    Posted 13 years ago #

    Ok so I am assuming by your answer you mean I need to query the DB to get the ID of the recent published blog post.

    $thispost = $wpdb->get_results("SELECT ID
    FROM $blogPostsTable WHERE post_status = 'publish'
    AND post_type = 'post' ORDER BY id DESC LIMIT 0,1");

    So would this work, or do I have to add in the date requirement. My only issue with the one provided is its taken right from ah_recent_posts which ignores posts that were not posted today but some bloggers do not post everyday, plus once midnight hits, there would be nothing to display til another post is made anyway.

  • DailyTestimony
    Member
    Posted 13 years ago #

    Alright so I am going to go off the assumption that the query will work since it does order by the ID, which the higher ID should be a newer post.

    The next question on this is how do I take the content of the post and take it down to say 200 words (if its longer)?

  • DailyTestimony
    Member
    Posted 13 years ago #

    So...any suggestions on how to take only the first 200 words if the post content is longer?

  • DailyTestimony
    Member
    Posted 13 years ago #

    Well I think I figured my last issue out but now I got this php error I cannot figure out

    Parse error: syntax error, unexpected T_VARIABLE in /home/dtnet/public_html/wp-content/mu-plugins/featured_posts.php on line 32

    Line 32: $blog_table = 'wp_' . $featured_blogs[$random] . '_options';

  • DailyTestimony
    Member
    Posted 13 years ago #

    Ok that was smart, I never ended the previous line with ; it's all good now...

  • DailyTestimony
    Member
    Posted 13 years ago #

    But now I got a new error
    Parse error: syntax error, unexpected $end in /home/dtnet/public_html/wp-content/mu-plugins/featured_posts.php on line 68

    Line 68 is the last line, my code is here
    http://dailytestimony.net/plugins/debug/featured_posts.txt

  • DailyTestimony
    Member
    Posted 13 years ago #

    anyone can tell why I get that error, I am stuck.

  • mrhenry
    Member
    Posted 13 years ago #

    I couldn't check your code but you're propably missing a "}" or ";" somewhere. Just browse carefully through the code and you'll find it.

  • DailyTestimony
    Member
    Posted 13 years ago #

    Well it was a couple of problems, a missing } was one of them thanks. But now I have another error:

    Fatal error: Call to a member function get_results() on a non-object in /home/dtnet/public_html/wp-content/mu-plugins/featured_post.php on line 20

    Line 20: $thispost = $wpdb->get_results("SELECT id FROM $blogPostsTable WHERE post_status = 'publish' AND post_type = 'post' ORDER BY id DESC LIMIT 0,1");

    Full code updated at http://www.dailytestimony.net/Plugins/debug/featured_posts.txt

  • DailyTestimony
    Member
    Posted 13 years ago #

    any ideas whats causing that?

  • MrBrian
    Member
    Posted 13 years ago #

    need this:
    global $wpdb;

    Your database object is empty.

  • DailyTestimony
    Member
    Posted 13 years ago #

    well thanks that fixed my error, and I haven't got anymore erros even, now if only I can get it to output what I want, right now it's outputing:

    Array on
    More...'
    Array>>>

    now the more... part it should dsiplay but the rest I don't know...I do wish I had a better understanding of PHP i'll look at it and see if I can see the issue.

  • MrBrian
    Member
    Posted 13 years ago #

    if you try to echo a variable that is an array, it will just say "Array" because you need to tell it what part of the array to echo using a loop or element. Paste your code and i can help

  • DailyTestimony
    Member
    Posted 13 years ago #

    function featured_post(){
    $featured_blogs = array(1,2); //Update this array with the ID's of featured blogs
    srand(time());
    $random = (rand()%2); //Update this with the number of entries in the array above
    
    global $wpdb;
    
    $thispost = $wpdb->get_results("SELECT id FROM $blogPostsTable WHERE post_status = 'publish' AND post_type = 'post' ORDER BY id DESC LIMIT 0,1");
    
    $featured_post = get_blog_post($featured_blogs[$random], $thispost);
    
    $post_author_id = $featured_post['post_author'];
    
    $post_author = $wpdb->get_results("SELECT display_name FROM wp_users WHERE id = $post_author_id");
    
    $permalink = get_blog_permalink($featured_blogs[$random], $thispost);
    
    $blog_url = get_blogaddress_by_id($featured_blogs[$random]);
    
    $blog_table = 'wp_' . $featured_blogs[$random] . '_options';
    
    $blog_name = $wpdb->get_results("SELECT option_value FROM $blog_table WHERE option_name = 'blogname'");
    
    $content = explode(' ',$featured_post['post_content']);
    for($i=0; $i<200; $i++){
    	$summary[$i] = $content[$i];
    }
    $summary = implode(' ', $summary);
    
    //Lets Output the Post ?>
    
    <div class="entry entry-1">
    <div class="entrytitle">
    <h1><a href="<?php echo $permalink; ?>" rel="bookmark" title="Link to <?php $featured_post['post_title']; ?>"><?php $featured_post['post_title']; ?></a></h1>
    <div class="endate"><?php echo $post_author ?> on <?php date('F jS, Y', $featured_post['post_date']); ?></div>
    </div>
    
    <div class="entrybody">
    <?php echo $summary ?><a href="<?php echo $permalink ?>" rel="bookmark" title="Link to <?php $featured_post['post_title'] ?>">More...</a>'
    </div>
    
    <p align=right>
    <span class="dotie">
    <a href="<?php echo $blog_url; ?>"><?php echo $blog_name . '>>>'; ?></a>
    </span>
    
    <?php } ?>

    Thanks for your help

  • politicalbear
    Member
    Posted 13 years ago #

    I am excited to use this code when it works. This is exactly what I have been trying to work out..........just not near as well as you Daily or what it looks like you are going to do for the community Mr.Brian.

    Thank you in advance.

    Also, would anybody recommend any sites or books for php or mysql. I have been learning, but I need a good resource. Beginner resource.

  • DailyTestimony
    Member
    Posted 13 years ago #

    as well as me? Heck I though together some bad code that dosen't even work and use the communitys knowledge to fix it...lol

  • DailyTestimony
    Member
    Posted 13 years ago #

    anybody see hat would cause it to not display??? It looks fine to me, but then again I don't know what to look for.

  • DailyTestimony
    Member
    Posted 13 years ago #

    Well I noticed a small inconsitancy in my code, not sure if it makes any diffrence but look at these two lines:

    <h1><a href="<?php echo $permalink; ?>" rel="bookmark" title="Link to <?php $featured_post['post_title']; ?>"><?php $featured_post['post_title']; ?></a></h1>

    <?php echo $summary ?><a href="<?php echo $permalink ?>" rel="bookmark" title="Link to <?php $featured_post['post_title'] ?>">More...</a>'

    I completley missed the ;'s in the second example...i'll let you know if that fixes it.

    EDIT: Well not I fixed all of those but now it seems i'm getting an error

    Fatal error: Call to a member function get_results() on a non-object in /home/dtnet/public_html/wp-content/mu-plugins/featured_post.php on line 20

  • DailyTestimony
    Member
    Posted 13 years ago #

    Oh oops...forgot to RTFP...it seems I already got the answer in this post but in resolving other issues the global $wpdb; vanished.

    EDIT: *bangs head* I get the feeling I'm going in circles with this code...now I'm back to displaying Array On.

  • infoclipper
    Member
    Posted 13 years ago #

    Post the entire function as it is now and I'll be happy to take a look for you.

  • DailyTestimony
    Member
    Posted 13 years ago #

    <?php
    /*
    Plugin Name: Featured-Post
    Plugin URI: http://dailytestimony.net/plugins/featured-post.php
    Description: Selects randomly one of the blogs provided and gets the most recent post from it
    Author: Daily Testimony
    Author URI: http://dailytestimony.net/
    License: Creative Commons Attribution-Share Alike 3.0 United States
    License URI: http://creativecommons.org/licenses/by-sa/3.0/us/
    Version: 0.1beta
    */
    
    function featured_post(){
    $featured_blogs = array(1,2); //ID's of featured blogs
    srand(time());
    $random = (rand()%2); //number of entries in the array above
    
    global $wpdb;
    
    $thispost = $wpdb->get_results("SELECT id FROM $blogPostsTable WHERE post_status = 'publish' AND post_type = 'post' ORDER BY id DESC LIMIT 0,1");
    
    $featured_post = get_blog_post($featured_blogs[$random], $thispost);
    
    $post_author_id = $featured_post['post_author'];
    
    $post_author = $wpdb->get_results("SELECT display_name FROM wp_users WHERE id = $post_author_id");
    
    $permalink = get_blog_permalink($featured_blogs[$random], $thispost);
    
    $blog_url = get_blogaddress_by_id($featured_blogs[$random]);
    
    $blog_table = 'wp_' . $featured_blogs[$random] . '_options';
    
    $blog_name = $wpdb->get_results("SELECT option_value FROM $blog_table WHERE option_name = 'blogname'");
    
    $content = explode(' ',$featured_post['post_content']);
    for($i=0; $i<200; $i++){
    	$summary[$i] = $content[$i];
    }
    $summary = implode(' ', $summary);
    
    //Lets Output the Post ?>
    
    <div class="entry entry-1">
    <div class="entrytitle">
    <h1><a href="<?php echo $permalink; ?>" rel="bookmark" title="Link to <?php $featured_post['post_title']; ?>"><?php $featured_post['post_title']; ?></a></h1>
    <div class="endate"><?php echo $post_author; ?> on <?php date('F jS, Y', $featured_post['post_date']); ?></div>
    </div>
    
    <div class="entrybody">
    <?php echo $summary; ?><a href="<?php echo $permalink; ?>" rel="bookmark" title="Link to <?php $featured_post['post_title']; ?>">More...</a>
    </div>
    
    <p align=right>
    <span class="dotie">
    <a href="<?php echo $blog_url; ?>"><?php echo $blog_name . '>>>'; ?></a>
    </span>
    
    <?php } ?>
  • infoclipper
    Member
    Posted 13 years ago #

    Untested, but you had a few variable issues. Try this one:

    <?php
    /*
    Plugin Name: Featured-Post
    Plugin URI: http://dailytestimony.net/plugins/featured-post.php
    Description: Selects randomly one of the blogs provided and gets the most recent post from it
    Author: Daily Testimony
    Author URI: http://dailytestimony.net/
    License: Creative Commons Attribution-Share Alike 3.0 United States
    License URI: http://creativecommons.org/licenses/by-sa/3.0/us/
    Version: 0.1beta
    */
    
    function featured_post(){
    	global $wpdb;
    
    	$featured_blogs = array(1,2); //Update this array with the ID's of featured blogs
    	srand(time()); //Do not change this
    	$random = (rand() % count($featured_blogs)); //Update this with the number of entries in the array above
    
    	$thispost = $wpdb->get_results("SELECT id FROM " . $wpdb->posts . " WHERE post_status = 'publish' AND post_type = 'post' ORDER BY id DESC LIMIT 0,1");
    
    	$featured_post = get_blog_post($featured_blogs[$random], $thispost);
    	$post_author_id = $featured_post['post_author'];
    	$post_author = $wpdb->get_results("SELECT display_name FROM wp_users WHERE id = $post_author_id");
    	$permalink = get_blog_permalink($featured_blogs[$random], $thispost);
    	$blog_url = get_blogaddress_by_id($featured_blogs[$random]);
    
    	$blog_table = 'wp_' . $featured_blogs[$random] . '_options';
    	$blog_name = $wpdb->get_results("SELECT option_value FROM $blog_table WHERE option_name = 'blogname'");
    
    	$content = explode(' ',$featured_post['post_content']);
    	for($i=0; $i<200; $i++){
    		$summary[$i] = $content[$i];
    	}
    	$summary = implode(' ', $summary);
    
    	//Lets Output the Post ?>
    	<div class="entry entry-1">
    	<div class="entrytitle">
    	<h1><a href="<?php echo $permalink; ?>" rel="bookmark" title="Link to <?php $featured_post['post_title']; ?>"><?php $featured_post['post_title']; ?></a></h1>
    	<div class="endate"><?php echo $post_author; ?> on <?php date('F jS, Y', $featured_post['post_date']); ?></div>
    	</div>
    
    	<div class="entrybody">
    	<?php echo $summary; ?><a href="<?php echo $permalink; ?>" rel="bookmark" title="Link to <?php $featured_post['post_title']; ?>">More...</a>
    	</div>
    
    	<p align=right>
    	<span class="dotie">
    	<a href="<?php echo $blog_url; ?>"><?php echo $blog_name . '>>>'; ?></a>
    	</span>
    	</p>
    <?php
    };
    ?>

    A lot of what you are doing isn't very optimal. For instance, to get the first 200 characters of a string, you only need to do:

    $summary = substr($content, 0, 200);

    If you want the first 200 words (as you have above), you can do it as:

    $summary = explode(" ", $content, 200);
    $summary = implode(" ", $summary);
  • DailyTestimony
    Member
    Posted 13 years ago #

    It still just says Array On, and well it displays the "more..." link.

    Also using the URL the more link goes to to figure out what post it grabbed, it grabbed the oldest post not the newest one. Thats simple just change the decend order to ascend.

    And I loaded the page again and now its getting the first post of blog 2 so the random number generation is working.

  • infoclipper
    Member
    Posted 13 years ago #

    Sorry; I had three things going on at once when I posted my previous message and didn't really pay that much attention beyond some variables that you had in the code that clearly wouldn't work inside a function.

    In actually reading your code, I'm confused about something-- get_blog_post actually returns an *object*, not an *array*.

    So I think that all of your lines that have something like $featured_post['post_author'] are suspect-- they should be in the form of $feature_post->post_author.

    You can check this by simply adding in some echoes after you set some of your variables:

    $featured_post = get_blog_post($featured_blogs[$random], $thispost);
    $post_author_id = $featured_post['post_author'];
    echo "post_author_id = $post_author_id";

    And see what's output. I'm betting dollars to donuts you won't be getting what you expect.

  • DailyTestimony
    Member
    Posted 13 years ago #

    I tried that now my code is

    <?php
    /*
    Plugin Name: Featured-Post
    Plugin URI: http://dailytestimony.net/plugins/featured-post.php
    Description: Selects randomly one of the blogs provided and gets the most recent post from it
    Author: Daily Testimony
    Author URI: http://dailytestimony.net/
    License: Creative Commons Attribution-Share Alike 3.0 United States
    License URI: http://creativecommons.org/licenses/by-sa/3.0/us/
    Version: 0.1beta
    */
    
    function featured_post(){
    	global $wpdb;
    
    	$featured_blogs = array(1,2); //Update this array with the ID's of featured blogs
    	srand(time()); //Do not change this
    	$random = (rand() % count($featured_blogs)); //Update this with the number of entries in the array above
    
    	$thispost = $wpdb->get_results("SELECT id FROM " . $wpdb->posts . " WHERE post_status = 'publish' AND post_type = 'post' ORDER BY id DESC LIMIT 0,1");
    
    	$featured_post = get_blog_post($featured_blogs[$random], $thispost);
    	$post_author_id = $feature_post->post_author;
    	$post_author = $wpdb->get_results("SELECT display_name FROM wp_users WHERE id = $post_author_id");
    	$permalink = get_blog_permalink($featured_blogs[$random], $thispost);
    	$blog_url = get_blogaddress_by_id($featured_blogs[$random]);
    
    	$blog_table = 'wp_' . $featured_blogs[$random] . '_options';
    	$blog_name = $wpdb->get_results("SELECT option_value FROM $blog_table WHERE option_name = 'blogname'");
    
    	$content = explode(' ',$feature_post->post_content);
    	for($i=0; $i<200; $i++){
    		$summary[$i] = $content[$i];
    	}
    	$summary = implode(' ', $summary);
    
    	//Lets Output the Post ?>
    	<div class="entry entry-1">
    	<div class="entrytitle">
    	<h1><a href="<?php echo $permalink; ?>" rel="bookmark" title="Link to <?php $feature_post->post_title; ?>"><?php $feature_post->post_title; ?></a></h1>
    	<div class="endate"><?php echo $post_author; ?> on <?php date('F jS, Y', $feature_post->post_date); ?></div>
    	</div>
    
    	<div class="entrybody">
    	<?php echo $summary; ?><a href="<?php echo $permalink; ?>" rel="bookmark" title="Link to <?php $feature_post->post_title; ?>">More...</a>
    	</div>
    
    	<p align=right>
    	<span class="dotie">
    	<a href="<?php echo $blog_url; ?>"><?php echo $blog_name . '>>>'; ?></a>
    	</span>
    	</p>
    <?php
    };
    ?>

    but the output remains the same.

  • DailyTestimony
    Member
    Posted 13 years ago #

    I think the issue may be with my queries, I noticed istead of (blog name)>>> the link to the blog displays Array>>> which is kinda odd, but it links to the right blog...so I am thinking the problem is not within my display code but with my querying code.

  • infoclipper
    Member
    Posted 13 years ago #

    If that's your code above, then one of the problems is that it's full of typos:

    $featured_post = get_blog_post($featured_blogs[$random], $thispost);
    $post_author_id = $feature_post->post_author;

    Notice how you set the return of get_blog_post to $featured_post -- with a "d". Check out the next line (and throughout the rest of the code)-- no "d".

  • 1 2 3

    About this Topic