Ask your WordPress questions! Pay money and get answers fast! (more info)

Multisite $wpdb query

  • SOLVED

Hello all,

I'm kind of confused as to what I'm doing wrong with my multisite $wpdb query. I had it working along time ago and now I can't find where I put the code. I'm sure why the $query variable isn't working in the foreach loop?

<em>Invalid argument supplied for foreach() </em>


[[LINK href="http://pastebin.com/DMKeRt8z"]]Here's the query[[/LINK]]

Thanks a lot!

<em>I'm almost 100% certain I had made this code work some how a while back, but can't find where I put the code</em>

Answers (2)

2014-10-16

zebra webdesigns answers:

Hello Corbin I have two suggestions for you.
1. Check the if the variable is array before passing it to foreach loop. Check the following link
http://stackoverflow.com/questions/2630013/invalid-argument-supplied-for-foreach

2. Use print_r($query); to see if it contains any values inside.

If you would like me to look in to this I can help you out. Shout me a PM , I will also available in skype : bhuvan530531


Chris comments:

Thank you very much for your help! I just got the answer on my own :)

2014-10-16

Hariprasad answers:

Hi,

You have to call <em>umw_get_blog_posts()</em> before <em>foreach()</em> to retrieve posts. Like this


<?php
$query = umw_get_blog_posts();
foreach ($query as $post): ?>


Chris comments:

Thanks Hariprasad,

<?php global $post; ?>
<?php $query = umw_get_blog_posts(); ?>
<?php foreach ($query as $post): ?>
<?php setup_postdata($post); ?>
<h2><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>">
<?php the_title(); ?></a></h2>
<?php endforeach; ?>


This got me a bunch of posts from the site I'm on and an error:

<em>Warning: Missing argument 2 for wpdb::prepare(), called in C:\xampp\htdocs\wordpress\wp-content\themes\Test Theme\test-template-2.php on line 21 and defined in C:\xampp\htdocs\wordpress\wp-includes\wp-db.php on line 1154</em>

I swear I got this to work before I just can't for the life of me remember what I did.

[[LINK href="http://www.htmlcenter.com/blog/wordpress-multi-site-switching-blogs/"]]Here's the site I pulled the function from[[/LINK]]

Thank you!


Hariprasad comments:

Try following code

<div class="entry-content">
<?php function umw_get_blog_posts( $blogid=1, $type='post', $parent=0 ) {
global $wpdb;
$wpdbobj = clone $wpdb;
$wpdb->blogid = $blogid;
$wpdb->set_prefix( $wpdb->base_prefix );
$query = $wpdb->prepare( "SELECT ID, post_title, post_name FROM $wpdb->posts WHERE post_status='publish' AND post_type='%s' AND post_parent=%d",$type,$parent );
$wpdb = clone $wpdbobj;
return $wpdb->get_results( $query, ARRAY_A );
} ?>
<?php global $post; ?>
<?php $query = umw_get_blog_posts(); ?>
<?php foreach ($query as $post): ?>
<?php setup_postdata($post); ?>
<h2><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>">
<?php the_title(); ?></a></h2>
<?php endforeach; ?>
</div>


Hariprasad comments:

Also try,

Move function function <em>umw_get_blog_posts( $blogid=1, $type='post', $parent=0 ) </em> to your functions.php and just use following to retrieve posts

<div class="entry-content">
<?php global $post; ?>
<?php $query = umw_get_blog_posts(); ?>
<?php foreach ($query as $post): ?>
<?php setup_postdata($post); ?>
<h2><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>">
<?php the_title(); ?></a></h2>
<?php endforeach; ?>
</div>


Chris comments:

Thanks,

I tried that. It got rid of the error, but it's still grabbing multiples of a one post "Hello World" over and over again from the blog that I'm on and not the site I'm on and not the site I'm trying to pull posts from.


Hariprasad comments:

You can pass parameters when you call <em>$query = umw_get_blog_posts();</em>

For Ex:
<?php $query = umw_get_blog_posts(1, 'post, 0); ?>

or just

<?php $query = umw_get_blog_posts(2); ?> // 2 is your blog id.



Chris comments:

I just tried:

<blockquote>Move function function umw_get_blog_posts( $blogid=1, $type='post', $parent=0 ) to your functions.php and just use following to retrieve posts</blockquote>

Still getting multiples of the same post


Chris comments:

<blockquote>You can pass parameters when you call $query = umw_get_blog_posts();</blockquote>

Just tried. Still have the same thing coming up


Chris comments:

Just for extra information. I'm on site 4 prefix wp_4 and trying to gather content from site 1


Hariprasad comments:

<blockquote>I'm on site 4 prefix wp_4 and trying to gather content from site 1</blockquote>

what you get when you try this?
<?php $query = umw_get_blog_posts(1, 'post, 0); ?>

Is it listing posts from site 1?

I didn't understand
<blockquote>Still getting multiples of the same post</blockquote>


Chris comments:

Sorry, it's listing the same "hello world" post from site 4 over and over again. When I'm trying to make a call to site 1.


Hariprasad comments:

Could you try this code

<div class="entry-content">
<?php
global $switched;
$blogid = 1;
$type = 'post';
$parent = 0;
switch_to_blog($blogid); //switched to main blog

// Get latest Post
$staff = query_posts(array(
'post_type' => $type,
'posts_per_page' => 10,
'post_parent' => $parent
)
);
?>
<?php if ( have_posts()) : ?>
<?php while ( have_posts() ) : the_post(); ?>
<h2><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>">
<?php endwhile; wp_reset_query(); ?>
<?php endif; ?>
<?php restore_current_blog(); //switched back to main site ?>
</div>


Chris comments:

That worked, but I didn’t want the overhead of using the switch_to_blog() function.

Thank you for your help so far. I shall be raising the money soon for this question.


Chris comments:

Thank you very much for your help! I just got the answer on my own :)