WordPress.org

Ready to get started?Download WordPress

Forums

Add Pagination to querystr (19 posts)

  1. dlishus
    Member
    Posted 1 year ago #

    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; ?>
  2. esmi
    Forum Moderator
    Posted 1 year ago #

    If you used a while... endwhile loop, you could probably insert the standard WordPress page navigation functions.

  3. dlishus
    Member
    Posted 1 year ago #

    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?

  4. esmi
    Forum Moderator
    Posted 1 year ago #

    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; ?>
  5. dlishus
    Member
    Posted 1 year ago #

    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.php

    Thank you for your help thus far!

  6. esmi
    Forum Moderator
    Posted 1 year ago #

    Remove $pageposts = $wpdb->get_results($querystr);?> and the final <?php endif; ?>.

  7. dlishus
    Member
    Posted 1 year ago #

    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; ?>
  8. esmi
    Forum Moderator
    Posted 1 year ago #

    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.

  9. dlishus
    Member
    Posted 1 year ago #

    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.

  10. esmi
    Forum Moderator
    Posted 1 year ago #

    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;?>
  11. curlybracket
    Member
    Posted 1 year ago #

    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.

  12. curlybracket
    Member
    Posted 1 year ago #

    Just found this too :

    $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;

    Maybe this helps.

  13. bcworkz
    Member
    Posted 1 year ago #

    I'm pretty sure curlybracket's helpful suggestions was meant to have a post_type line like this:
    'post_type' => array('announcement', 'audio_ann'),

  14. dlishus
    Member
    Posted 1 year ago #

    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.

  15. keesiemeijer
    moderator
    Posted 1 year ago #

    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-pagenavi

    You will have to change your custom fields (date_of_event) to to the new format for this to work.

  16. dlishus
    Member
    Posted 1 year ago #

    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 28

    Line 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!

  17. keesiemeijer
    moderator
    Posted 1 year ago #

    That's strange, I don't get any errors. Can you paste the full code of the template file here.

  18. dlishus
    Member
    Posted 1 year ago #

    I took another look and played around with the original code you supplied. I removed the lines in bold and it's working perfectly! My posts are sorted by date of event and paginated.

    <?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);
    ?>

    THANK YOU!

  19. keesiemeijer
    moderator
    Posted 1 year ago #

    You're welcome. I'm glad you got it resolved.

Topic Closed

This topic has been closed to new replies.

About this Topic