This is site is an etsy-type site where users can sell art. I'm using Marketpress from WPMUDEV.org for products. It's done using a custom post type.
I need to be able to display random products (posts from this particular post type) from ALL blogs onto the home page of the site. Basically, the following:
<a href="<?php the_permalink() ?>" title="<?php the_title(); ?>"><?php the_post_thumbnail( 'thumbnail'); ?></a>
<a href="<?php the_permalink() ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a>
<?php the_author(); ?>
the post_type = 'product'
EDIT: I've increased this question to $20.
Honzik A. answers:
Please try with this [[LINK href="http://wordpress.org/extend/plugins/igit-related-posts-with-thumb-images-after-posts/"]]plugin[[/LINK]].
Jake Caputo comments:
That doesn't really do anything I'm looking for. And I'm not looking for a plugin.
Sébastien | French WordpressDesigner answers:
<?php
global $wpdb;
global $blog_id;
$table_prefix = $wpdb->base_prefix;
$sqlstr = '';
$blog_list = get_blog_list( 0, 'all' );
$sqlstr = "SELECT * from ".$table_prefix ."posts where post_status = 'publish' and post_type = 'post' ";
$uni = '';
foreach ($blog_list AS $blog) {
if ($blog['blog_id'] != 1) {
if ($sqlstr != '')
$uni = ' union ';;
$sqlstr .= $uni . " SELECT * from ".$table_prefix .$blog['blog_id']."_posts where post_status = 'publish' and post_type = 'post' ";
}
}
$limit = '';//' LIMIT 0, 5';
$sqlstr .= " ORDER BY RAND()";
$post_list = $wpdb->get_results($sqlstr);
//echo $wpdb->print_error();
//print_r($post_list);
foreach ($post_list as $article) {
echo '<a href="'.get_permalink($article->ID).'" title="'. $article->post_title .'">'. get_the_post_thumbnail($article->ID) .'</a>';
echo '<a href="'.get_permalink($article->ID).'" title="'. $article->post_title .'">'. $article->post_title .'</a>';
echo $article->post_author;
}
?>
Sébastien | French WordpressDesigner comments:
in your case, replace
post_type = 'post'
by
post_type = 'product'
if product is the name of your posttype
Jake Caputo comments:
This seems to be working somewhat. It's giving me the following:
<a title="Test Painting" href=""></a>
<a title="Test Painting" href="">TestPainting</a>
3
So it's getting the title and the author ID number, but not any links or the post_thumbnail.
Sébastien | French WordpressDesigner comments:
is it ok ?
Sébastien | French WordpressDesigner comments:
try this code (replace in post_type = 'post' post by your customtype > product ?)
<?php
if( !function_exists( 'get_the_post_thumbnail_by_blog' ) ) {
function get_the_post_thumbnail_by_blog($blog_id=NULL,$post_id=NULL,$size='post-thumbnail',$attrs=NULL) {
global $current_blog;
$sameblog = false;
if( empty( $blog_id ) || $blog_id == $current_blog->ID ) {
$blog_id = $current_blog->ID;
$sameblog = true;
}
if( empty( $post_id ) ) {
global $post;
$post_id = $post->ID;
}
if( $sameblog )
return get_the_post_thumbnail( $post_id, $size, $attrs );
if( !has_post_thumbnail_by_blog($blog_id,$post_id) )
return false;
global $wpdb;
$oldblog = $wpdb->set_blog_id( $blog_id );
$blogdetails = get_blog_details( $blog_id );
$thumbcode = str_replace( $current_blog->domain . $current_blog->path, $blogdetails->domain . $blogdetails->path, get_the_post_thumbnail( $post_id, $size, $attrs ) );
$wpdb->set_blog_id( $oldblog );
return $thumbcode;
}
function has_post_thumbnail_by_blog($blog_id=NULL,$post_id=NULL) {
if( empty( $blog_id ) ) {
global $current_blog;
$blog_id = $current_blog;
}
if( empty( $post_id ) ) {
global $post;
$post_id = $post->ID;
}
global $wpdb;
$oldblog = $wpdb->set_blog_id( $blog_id );
$thumbid = has_post_thumbnail( $post_id );
$wpdb->set_blog_id( $oldblog );
return ($thumbid !== false) ? true : false;
}
function the_post_thumbnail_by_blog($blog_id=NULL,$post_id=NULL,$size='post-thumbnail',$attrs=NULL) {
echo get_the_post_thumbnail_by_blog($blog_id,$post_id,$size,$attrs);
}
}
global $wpdb;
global $blog_id;
$table_prefix = $wpdb->base_prefix;
$sqlstr = '';
$blog_list = get_blog_list( 0, 'all' );
$sqlstr = "SELECT * from ".$table_prefix ."posts where post_status = 'publish' and post_type = 'post' ";
$uni = '';
foreach ($blog_list AS $blog) {
if ($blog['blog_id'] != 1) {
if ($sqlstr != '')
$uni = ' union ';;
$sqlstr .= $uni . " SELECT * from ".$table_prefix .$blog['blog_id']."_posts where post_status = 'publish' and post_type = 'post' ";
}
}
$limit = '';//' LIMIT 0, 5';
$sqlstr .= " ORDER BY RAND()";
$post_list = $wpdb->get_results($sqlstr);
//echo $wpdb->print_error();
//print_r($post_list);
foreach ($post_list as $article) {
$image=the_post_thumbnail_by_blog($blog['blog_id'],$article->ID,$size='post-thumbnail',$attrs=NULL);
echo '<a href="' . get_permalink($article->ID) . '" title="'. $article->post_title .'">'. $image .'</a>';
echo '<a href="' .get_permalink($article->ID). '" title="'. $article->post_title .'">'. $article->post_title .'</a>';
echo $article->post_author;
}
?>
Jake Caputo comments:
It seems to be getting the posts, but not displaying the information. Any ideas?
Jake Caputo comments:
oops. we keep responding at the same time. Let me give that last bit a try =)
Jake Caputo comments:
Nope. It's still outputting
<a title="Test Painting" href=""></a>
<a title="Test Painting" href="">Test Painting</a>
3
(and yes, post_type='product')
Sébastien | French WordpressDesigner comments:
could you try with
post_type = 'post'
and no
post_type = 'product'
Sébastien | French WordpressDesigner comments:
i see one error
replace
$image=the_post_thumbnail_by_blog($blog['blog_id'],$article->ID,$size='post-thumbnail',$attrs=NULL);
echo '<a href="' . get_permalink($article->ID) . '" title="'. $article->post_title .'">'. $image .'</a>';
by
echo '<a href="' . get_permalink($article->ID) . '" title="'. $article->post_title .'">';
the_post_thumbnail_by_blog($blog['blog_id'],$article->ID,$size='post-thumbnail',$attrs=NULL);
echo '</a>';
Jake Caputo comments:
I did the last replace you said and I was still getting the same output.
When I replaced 'product' with 'post' it gave me this:
<a title="Hello world!" href="http://www.artgalleree.com/blog/2011/02/16/hello-world/"></a>
<a title="Hello world!" href="http://www.artgalleree.com/blog/2011/02/16/hello-world/">Hello world!</a>
4
<a title="Hello world!" href="http://www.artgalleree.com/blog/2011/02/16/hello-world/"></a>
<a title="Hello world!" href="http://www.artgalleree.com/blog/2011/02/16/hello-world/">Hello world!</a>
2
<a title="Hello world!" href="http://www.artgalleree.com/blog/2011/02/16/hello-world/"></a>
<a title="Hello world!" href="http://www.artgalleree.com/blog/2011/02/16/hello-world/">Hello world!</a>
3
Sébastien | French WordpressDesigner comments:
arf... sorry, it seems that doesn't work...
Sébastien | French WordpressDesigner comments:
i think i can return the 10 blogs last updated and in ecah blog i can display the more recent product
is it interesting for you ?
Jake Caputo comments:
No that won't do me any good. The users will only have a few products, and won't be updating too often.
Duncan O'Neill answers:
Sebastien's last replace is not printing the link content, and may work with two simple fixes;
echo '<a href="' . get_permalink($article->ID) . '" title="'. $article->post_title .'">';
the_post_thumbnail_by_blog($blog['blog_id'],$article->ID,$size='post-thumbnail',$attrs=NULL);
echo '</a>';
Try changing this;
function the_post_thumbnail_by_blog($blog_id=NULL,$post_id=NULL,$size='post-thumbnail',$attrs=NULL) {
echo get_the_post_thumbnail_by_blog($blog_id,$post_id,$size,$attrs);
}
to this;
function the_post_thumbnail_by_blog($blog_id=NULL,$post_id=NULL,$size='post-thumbnail',$attrs=NULL) {
return get_the_post_thumbnail_by_blog($blog_id,$post_id,$size,$attrs);
}
and the first piece of code to this;
echo '<a href="' . get_permalink($article->ID) . '" title="'. $article->post_title .'">';
echo the_post_thumbnail_by_blog($blog['blog_id'],$article->ID,$size='post-thumbnail',$attrs=NULL);
echo '</a>';
Jake Caputo comments:
No go. I'm still getting the same output.
<a title="Test Painting" href=""></a>
<a title="Test Painting" href="">Test Painting</a>
3
Denzel Chia answers:
Hi,
Found this function in one of the free plugins in wordpress repository.
<?php
/*
Parameter explanations
$how_many: how many recent posts are being displayed
$how_long: time frame to choose recent posts from (in days), set to 0 to disable
$titleOnly: true (only title of post is displayed) OR false (title of post and name of blog are displayed)
$begin_wrap: customise the start html code to adapt to different themes
$end_wrap: customise the end html code to adapt to different themes
Sample call: wpmu_get_recent_posts(5, 30, true, '<div>', '</div>'); >> 5 most recent entries over the past 30 days, displaying titles only
*/
function wpmu_get_recent_posts($how_many=10, $how_long=0, $titleOnly=true, $begin_wrap, $end_wrap) {
global $wpdb;
global $table_prefix;
$counter = 0;
// get a list of blogs in order of most recent update. show only public and nonarchived/spam/mature/deleted
if ($how_long > 0) {
$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 $how_long DAY)
ORDER BY last_updated DESC");
} else {
$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'
ORDER BY last_updated DESC");
}
if ($blogs) {
foreach ($blogs as $blog) {
// we need _posts and _options tables for this to work
$blogOptionsTable = $wpdb->base_prefix.$blog."_options";
$blogPostsTable = $wpdb->base_prefix.$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 ID for the latest post
if ($how_long > 0) {
$thispost = $wpdb->get_results("SELECT ID, post_title
FROM $blogPostsTable WHERE post_status = 'publish'
AND ID > 1
AND post_type = 'post'
AND post_date >= DATE_SUB(CURRENT_DATE(), INTERVAL $how_long DAY)
ORDER BY id DESC LIMIT 0,1");
} else {
$thispost = $wpdb->get_results("SELECT ID, post_title
FROM $blogPostsTable WHERE post_status = 'publish'
AND ID > 1
AND post_type = 'post'
ORDER BY id DESC LIMIT 0,1");
}
// if it is found put it to the output
if($thispost) {
// get permalink by ID. check wp-includes/wpmu-functions.php
$thispermalink = get_blog_permalink($blog, $thispost[0]->ID);
if ($titleOnly == false) {
echo $begin_wrap.'<a href="'
.$thispermalink.'">'.$thispost[0]->post_title.'</a> <br/> by <a href="'
.$options[0]->option_value.'">'
.$options[1]->option_value.'</a>'.$end_wrap;
$counter++;
} else {
echo $begin_wrap.'<a href="'.$thispermalink
.'">'.$thispost[0]->post_title.'</a>'.$end_wrap;
$counter++;
}
}
// don't go over the limit
if($counter >= $how_many) {
break;
}
}
}
}
?>
Sample usage
<?php wpmu_get_recent_posts(5, 30, true, '<div>', '</div>'); ?>
Jake Caputo comments:
This is working, but it only display one post per user, and still needs the thumbnail.