webmasters
webmasters

Reputation: 5831

Wordpress, external query inside the loop

$qry = mysql_query("SELECT test1,test2 FROM ".$table." ORDER BY RAND() LIMIT 6"); 

        $start =  new WP_Query('showposts=6&orderby=rand'); 

        if ($start->have_posts()) : while( $start->have_posts() && $rows = mysql_fetch_assoc($qry) ) : $start->the_post();

        $test1 = $rows['test1'];

        $test2 = $rows['test2']; 

I can manipulate the wordpress loop like this.... The problem appears when my table does not have 6 values inside, it happenes sometimes. Then, my index page doesn't show all the posts. For example, if i have 3 entries inside the table, than the loop displays only 3 posts instead of 6.

It would be great if i can make the query repeat itself... to supply the loop.

Any ideeas?

Upvotes: 0

Views: 545

Answers (2)

vinhboy
vinhboy

Reputation: 8982

$qry = mysql_query("SELECT test1,test2 FROM ".$table." ORDER BY RAND() LIMIT 6"); 

$start =  new WP_Query('showposts=6&orderby=rand'); 

if ($start->have_posts()) : while( $start->have_posts() ) : $start->the_post();
  $rows = mysql_fetch_assoc($qry)
  if (!$rows) {
    mysql_data_seek($qry,0);
    $rows = mysql_fetch_assoc($qry);
  }

Upvotes: 1

Dylan West
Dylan West

Reputation: 602

Probably not the most efficient answer, but you could do a query before this one to get the number of entries in the table and then put that in the 'showposts=$numposts' part of the query you have here.

Upvotes: 1

Related Questions