Add Pagination to querystr
-
I have a query combining two post types, and sorting by a custom field which goes by date. I’m lost on how to add page navigation. I’ve installed wp-page-navi and can’t seem to get it to work within my code.
<?php //Get today date $today = strtotime("now"); //Revised query string $querystr = " SELECT wposts.* FROM $wpdb->posts wposts, $wpdb->postmeta wpostmeta WHERE wposts.ID = wpostmeta.post_id AND wpostmeta.meta_key = 'date_of_event' AND wpostmeta.meta_value > '" . date("m-d-Y", $today) . "' AND wposts.post_status = 'publish' AND wposts.post_type IN ('annnouncement', 'audio_ann') ORDER BY wpostmeta.meta_value ASC LIMIT 0, 20 "; $dateIndexes = array(); $pageposts = $wpdb->get_results($querystr); ?> <?php if ($pageposts): ?> <ul> <?php foreach ($pageposts as $post): ?> <?php setup_postdata($post);?> <article class="cat_article "> //html for posts goes here </article> <!--End Cat Article--> <?php endforeach; ?> </ul> <?php else : ?> <p>No upcoming events</p> <?php endif; ?>
-
If you used a
while... endwhileloop, you could probably insert the standard WordPress page navigation functions.I’m not a php expert, so I’m not sure how to change it. I tried switching out foreach with while, and then endforeach with endwhile and that didn’t work. Is this something you could help me with please?
Try:
$pageposts = $wpdb->get_results($querystr);?> <?php if ($pageposts->have_posts()) : ?> <ul> <?php while (have_posts()) : the_post(); ?> <article class="cat_article "> //html for posts goes here </article> <!--End Cat Article--> <?php endwhile; ?> </ul> <?php else : ?> <p>No upcoming events</p> <?php endif; ?>This is how I pasted it in:
<?php //Get today date $today = strtotime("now"); //Revised query string $querystr = " SELECT wposts.* FROM $wpdb->posts wposts, $wpdb->postmeta wpostmeta WHERE wposts.ID = wpostmeta.post_id AND wpostmeta.meta_key = 'date_of_event' AND wpostmeta.meta_value > '" . date("m-d-Y", $today) . "' AND wposts.post_status = 'publish' AND wposts.post_type IN ('annnouncement', 'audio_ann') ORDER BY wpostmeta.meta_value ASC LIMIT 0, 20 "; $dateIndexes = array(); $pageposts = $wpdb->get_results($querystr);?> <?php if ($pageposts->have_posts()) : ?> <ul> <?php while (have_posts()) : the_post(); ?> <article class="cat_article "> //html goes here </article> <!--End Cat Article--> <?php endwhile; ?> </ul> <?php else : ?> <p>No upcoming events</p> <?php endif; ?>And I’m not getting any posts to display. My error log says
PHP Fatal error: Call to a member function have_posts() on a non-object in /path/to/front-page.phpThank you for your help thus far!
Remove
$pageposts = $wpdb->get_results($querystr);?>and the final<?php endif; ?>.No luck, I’m getting the same issue “Call to a member function have_posts() on a non-object”
The white screen of death doesn’t show, but I tried this: Taking off the endif resulted in a white screen, and removing the “$pageposts = $wpdb->get_results($querystr);?>” made it so there was an open that from $dateIndexes
<?php //Get today date $today = strtotime("now"); //Revised query string $querystr = " SELECT wposts.* FROM $wpdb->posts wposts, $wpdb->postmeta wpostmeta WHERE wposts.ID = wpostmeta.post_id AND wpostmeta.meta_key = 'date_of_event' AND wpostmeta.meta_value > '" . date("m-d-Y", $today) . "' AND wposts.post_status = 'publish' AND wposts.post_type IN ('annnouncement', 'audio_ann') ORDER BY wpostmeta.meta_value ASC LIMIT 0, 20 "; $dateIndexes = array(); ?> <?php if ($pageposts->have_posts()) : ?> <ul> <?php while (have_posts()) : the_post(); ?> <article class="cat_article "> test </article> <!--End Cat Article--> <?php endwhile; endif; ?>Can you do a dump of $pageposts and see what is actually being returned? I’m also curious as to why you’re building your own query string as opposed to use WP_Query(). The latter would make life a lot easier.
My code looks like this:
<?php //Get today date $today = strtotime("now"); //Revised query string $querystr = " SELECT wposts.* FROM $wpdb->posts wposts, $wpdb->postmeta wpostmeta WHERE wposts.ID = wpostmeta.post_id AND wpostmeta.meta_key = 'date_of_event' AND wpostmeta.meta_value > '" . date("m-d-Y", $today) . "' AND wposts.post_status = 'publish' AND wposts.post_type IN ('annnouncement', 'audio_ann') ORDER BY wpostmeta.meta_value ASC LIMIT 0, 20 "; $pageposts = $wpdb->get_results($querystr); var_dump($pageposts); $dateIndexes = array(); ?> <?php if ($pageposts->have_posts()) : ?> <ul> <?php while (have_posts()) : the_post(); ?> <article class="cat_article "> test </article> <!--End Cat Article--> <?php endwhile; endif; ?>And I am getting values returned, see here
I’m using a custom query because I need to query two custom post types, which are sorted by date of an event based on a custom field.
Try:
<?php //Get today date $today = strtotime("now"); //Revised query string $querystr = " SELECT wposts.* FROM $wpdb->posts wposts, $wpdb->postmeta wpostmeta WHERE wposts.ID = wpostmeta.post_id AND wpostmeta.meta_key = 'date_of_event' AND wpostmeta.meta_value > '" . date("m-d-Y", $today) . "' AND wposts.post_status = 'publish' AND wposts.post_type IN ('annnouncement', 'audio_ann') ORDER BY wpostmeta.meta_value ASC LIMIT 0, 20 "; $dateIndexes = array(); ?> <ul> <?php while (have_posts()) : the_post(); ?> <article class="cat_article "> test </article> <!--End Cat Article--> <?php endwhile;?>I suggest you use WP incorporated functions, something like this :
$args = array( 'posts_per_page' => '20', 'post_type' => 'announcement', 'paged' => $paged, 'meta_key' => 'date_of_event', 'orderby' => 'meta_value_num', 'order'=>'DESC' ); $myposts = new WP_Query($args); while ($myposts->have_posts()) : $myposts->the_post(); get_template_part('content_post'); // or print_r($post); endwhile; wp_reset_postdata();Check reference on meta query here : http://codex.wordpress.org/Class_Reference/WP_Query
And here http://www.wphub.com/use-meta_query-query-posts-postmeta/ if you need to compare the date to something.Just found this too :
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;Maybe this helps.
I’m pretty sure curlybracket’s helpful suggestions was meant to have a post_type line like this:
'post_type' => array('announcement', 'audio_ann'),esmi, I posted in the code you provided and am only getting the page title returned, not the title of the posts.
I had to add an endif (which I needed anyways for pagenavi) or else I got a white screen<?php //Get today date $today = strtotime("now"); //Revised query string $querystr = " SELECT wposts.* FROM $wpdb->posts wposts, $wpdb->postmeta wpostmeta WHERE wposts.ID = wpostmeta.post_id AND wpostmeta.meta_key = 'date_of_event' AND wpostmeta.meta_value > '" . date("m-d-Y", $today) . "' AND wposts.post_status = 'publish' AND wposts.post_type IN ('annnouncement', 'audio_ann') ORDER BY wpostmeta.meta_value ASC LIMIT 0, 20 "; $dateIndexes = array(); ?> <ul> <?php while (have_posts()) : the_post(); ?> <article class="cat_article "> <?php the_title(); ?> </article> <!--End Cat Article--> <?php endwhile;?> <?php if(function_exists('wp_pagenavi')) : wp_pagenavi(); endif; wp_reset_query();?>Curly Bracket, I appreciate the code – but I wasn’t able to get it working either. I’ve added the code as you’ve supplied and did a var_dump on $myposts if you’d like to take a look on what’s getting returned.
When sorting on date you have to use this format ‘Y-m-d’ for your custom field. You also can give wp-pagenavi the query you want to paginate. try it with a normal query [untested]:
<?php //Get today date $today = strtotime("now"); $paged = (get_query_var('paged')) ? get_query_var('paged') : 1; $args = array ( 'posts_per_page' => '20', 'post_type' => array('annnouncement', 'audio_ann'), 'paged' => $paged, 'meta_query'=> array( array( 'key' => 'date_of_event', 'value' => date("Y-m-d", $today), 'type' => 'DATE', 'compare' => '>', ) ), 'meta_key' => 'date_of_event', 'orderby' => 'meta_value', ); $the_query = new WP_Query($args); ?> <?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?> <article class="cat_article "> <?php the_title(); ?> </article> <!--End Cat Article--> <?php endwhile; ?> <?php if(function_exists('wp_pagenavi')) : wp_pagenavi( array( 'query' => $the_query ) ); endif; ?> <?php wp_reset_postdata(); ?>http://codex.wordpress.org/Function_Reference/WP_Query#Custom_Field_Parameters
http://codex.wordpress.org/Pagination#Troubleshooting_wp-pagenaviYou will have to change your custom fields (date_of_event) to to the new format for this to work.
So close keesiemeijer! If I strip out the array for sorting by date, the posts appear with pagination. Thank you so much!
However, I still need to sort by date. When I placed the full code in, I got this error:
PHP Parse error: syntax error, unexpected T_VARIABLE in /path/to/file on line 28Line 28 is:
$the_query = new WP_Query($args);Therefore, I think this is something small with the syntax. Can you help me with that? I really appreciate it!
The topic ‘Add Pagination to querystr’ is closed to new replies.