Hi I have made an sql query that gets several posts according the parameters that i've selected. I'm showing these posts but the inner paging within wordpress doesnt do anything. It shows me that there are more pages but when i select the page number it shows me the same results as before. That makes sense because it probably runs the sql query again. I'm using WordPress and BuddyPress. How can i page between these query results?

UPDATE: here is the code im using

$sql = "SELECT post_title, post_date, post_excerpt, guid, ID FROM wp_posts,wp_term_taxonomy, wp_terms, wp_term_relationships WHERE (post_status = 'publish' or post_status = 'inherit') and wp_term_taxonomy.term_taxonomy_id = ".$cat." and wp_posts.ID = wp_term_relationships.object_id and wp_terms.term_id = wp_term_taxonomy.term_taxonomy_id and wp_term_relationships.term_taxonomy_id = wp_term_taxonomy.term_taxonomy_id and wp_term_taxonomy.taxonomy = 'category' ORDER by post_date DESC";
$matching_posts = $wpdb->get_results($sql,OBJECT);
<?php if ( have_posts() ) : ?>

  <?php foreach ($matching_posts as $post): ?>
  <?php setup_postdata($post);

then im showing the info i want like title.

  • Is this related to your other question about sorting? Why do you have to use a custom SQL query? If you do that, you have to add the page handling code (LIMIT) yourself. – Jan Fabry Oct 27 '10 at 10:26
  • Hi @Jan, this is a different question. two different queries (altough they appear the same). how can i add the page handling code? i've used LIMIT but when the page numbers are shown they show me the same results. – peroxide Oct 27 '10 at 11:04
  • 1
    If you use the regular next_post_link() function I think it uses the $wp_query object to know how much more objects there are. So just using setup_postdata() with results from your own query will probably not replace the standard query for these functions, resulting in incorrect page numbers (saying there is a next page when there isn't and vice-versa). – Jan Fabry Oct 27 '10 at 12:07

You need to use get_posts(), have_posts(), and the_post() for the API functions to work.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.