Skip to:
Content
Pages
Categories
Search
Top
Bottom

Re: Display latest post from WP


nickbrady
Member

@nickbrady

Hi,

I am writing a plugin for BBPress that shows the latest posts from WP (if WP and BBPress are sharing the same database). It’s still in development, but it already works, you just have to write


<?php wp_show_posts(\"\" ); ?>

in any BBpress template.

As you will see, it works, but it has a problem: it does not show WP’s pretty URLs. I would like to call WP’s get_permalink() function, but it uses many WP functions that are not available when you’re in BBPress.

Any advice on how to do this? Or maybe it would be easier to read WP’s RSS?

Thanks!

Here goes the code of the plugin so far (if you don’t care about pretty permalinks, you can use it, it works):


<?php
/*
Plugin Name: WP Posts
Plugin URI: http://thesandbox.wordpress.com
Description: Get a list of WordPress posts from your bbPress (needs to be integrated with WP, sharing database)
Author: Nick Brady
Version: 0.1
Author URI: http://thesandbox.wordpress.com

Install Instructions:
- If you don't have a /my-plugins/ directory in your bbpress installaltion, create it on the same level as config.php.

- Check out: https://codex.wordpress.org/Template_Tags/get_posts

*/

function wp_get_posts($args) {
global $bbdb, $bb;

parse_str($args, $r);
if ( !isset($r['numberposts']) )
$r['numberposts'] = 5;
if ( !isset($r['offset']) )
$r['offset'] = 0;
if ( !isset($r['category']) )
$r['category'] = '';
if ( !isset($r['orderby']) )
$r['orderby'] = 'post_date';
if ( !isset($r['order']) )
$r['order'] = 'DESC';

$now = bb_current_time('mysql');

$posts = $bbdb->get_results(
\"SELECT DISTINCT * FROM \".$bb->wp_table_prefix.\"posts \" .
( empty( $r['category'] ) ? \"\" : \", \".$bb->wp_table_prefix.\"post2cat \" ) .
\" WHERE post_date <= '$now' AND (post_status = 'publish') \".
( empty( $r['category'] ) ? \"\" : \"AND \".$bb->wp_table_prefix.\"posts.ID = \".$bb->wp_table_prefix.\"post2cat.post_id AND \".$bb->wp_table_prefix.\"post2cat.category_id = \" . $r['category']. \" \" ) .
\" GROUP BY \".$bb->wp_table_prefix.\"posts.ID ORDER BY \" . $r['orderby'] . \" \" . $r['order'] . \" LIMIT \" . $r['offset'] . ',' . $r['numberposts'] );

return $posts;
}

function wp_show_posts( $args ) {
global $bb;
$posts = wp_get_posts( $args );
foreach( $posts as $post ) {
echo \"<li>n\";
echo \"<a href='\".$bb->wp_home.\"?p=\".$post->ID.\"'>\";
echo $post->post_title;
echo \"</a><br/>n\";
echo \"</li>n\";
}
}
?>

Skip to toolbar