开发者

Custom wordpress query help

I'm trying to build a custom wordpress query for a page that pulls the top voted articles from a one-off designed voting plugin from the DB. After every two articles I'm trying to insert something, via count. I also need pagination support. So far my efforts have been pretty bad, it's kinda working as I've tried below but still screwed up, and I have no idea how pagination fits in. Complete coding newbie here obviously.

so what I'm trying to accomplish is...

  • Grab top voted posts & order them.
  • After every two posts/articles,
  • insert extra content, using a count.
  • include pagination support, needed for my many pages of content.

`

$query_sql = "SELECT like_pid FROM " . $wpdb->prefix ."likes_count ORDER BY like_count D开发者_StackOverflowESC";
$query_result = $wpdb->get_col( $wpdb->prepare ($query_sql, OBJECT));
if ($query_result) {
foreach ($query_result as $post_id) {
$post = &get_post( $post_id );
setup_postdata($post);
?>

// Now count out two articles (more fail code?)

<?php $count++; ?>
<?php if ($count%2== 0) : ?>

// Show the two articles {more code}

<?php else : ?>

//Now do something else {more code}

<?php endif;?>
<?php } ?>
<?php } ?>

<div class="next"> <?php next_posts_link('&raquo;' ,0); ?></div>
<div class="previous"> <?php previous_posts_link('&laquo;' ,0); ?>`

http://pastebin.com/7dezgm92

Thank you for any advice or tips!


For pagination, you need a limit/offset:

ORDER BY like_count DESC LIMIT 10 OFFSET 0

The documented way to do this is to store the count total in postmeta, and to use the WP_Query object to order by that meta key/value and worry about the pagination.

http://codex.wordpress.org/Function_Reference/WP_Query

It'll be slow, however, because you'll end up doing a full table scan.

More ideally, add a count total column to the posts, an index on it, and then use the WP_Query object to query your posts and order them by that new column. You'll get the best results that way.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜