WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] how to make custom query paginate? (7 posts)

  1. cimenta
    Member
    Posted 4 years ago #

    hi

    I have a code that lists topics from a category on a page. It works nicely but I cannot make it paginate. I tried everything but I cannot make it work. Any suggestions?

    Thank you

    R

    function list_all_posts() {
    if (is_page('video')) {
      $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
      $args=array(
        'orderby' => 'date',
        'order' => 'DESC',
        'category_name' => 'blog',
        'posts_per_page' => 3,
        'post_status' => 'publish',
        'paged' => $paged,
      );
      $archive_query = new WP_Query($args);
      echo "<ul>\n";
      while ($archive_query->have_posts()) :     $archive_query->the_post();
        echo '<li><a href="' . get_permalink() . '" rel="bookmark">' . get_the_title() . "</a></li>\n";
      endwhile;
      echo "</ul>\n";
      wp_reset_query();
      }
    }
  2. MichaelH
    Member
    Posted 4 years ago #

    Review the "A page of posts" example in the Pages article.

  3. cimenta
    Member
    Posted 4 years ago #

    thank you Michael. I tried but ... It did not work. I slightly modified the code. I believe that I did not change anything important. The html code for page navigation is there but empty. It is like next_posts_link returns nothing.

    I used custom template for that page. It displays two posts. That is correct but the pagination ...

    if (is_page('audio')) {
      $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
      $post_per_page = 2; // -1 shows all posts
      $do_not_show_stickies = 1; // 0 to show stickies
      $args=array(
    	'category_name' => 'blog',
        'orderby' => 'date',
        'order' => 'DESC',
        'paged' => $paged,
        'posts_per_page' => $post_per_page,
        'caller_get_posts' => $do_not_show_stickies,
      );
      $temp = $wp_query;  // assign orginal query to temp variable for later use
      $wp_query = null;
      $wp_query = new WP_Query($args);
    	//echo var_dump($wp_query);
      if( have_posts() ) :
    		while ($wp_query->have_posts()) : $wp_query->the_post(); ?>
    	    <div <?php post_class() ?> id="post-<?php the_ID(); ?>">
            <h2><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></h2>
            <small><?php the_time('F jS, Y') ?> <!-- by <?php the_author() ?> --></small>
            <div class="entry">
              <?php the_content('Read the rest of this entry »'); ?>
            </div>
            <p class="postmetadata"><?php the_tags('Tags: ', ', ', '<br />'); ?> Posted in <?php the_category(', ') ?> | <?php edit_post_link('Edit', '', ' | '); ?>  <?php comments_popup_link('No Comments »', '1 Comment »', '% Comments »'); ?></p>
          </div>
        <?php endwhile; ?>
        <div class="navigation">
          <div class="alignleft"><?php next_posts_link('« Older Entries') ?></div>
          <div class="alignright"><?php previous_posts_link('Newer Entries »') ?></div>
        </div>
      <?php else : ?>
    
    		<h2 class="center">Not Found</h2>
    		<p class="center">Sorry, but you are looking for something that isn't here.</p>
    		<?php get_search_form(); ?>
    
    	<?php endif; 
    
    	$wp_query = $temp;  //reset back to original query
        }
  4. alchymyth
    The Sweeper & Moderator
    Posted 4 years ago #

    if this code is in a function, try to add
    global $post;
    before the
    if (is_page('audio')) {
    line.

  5. cimenta
    Member
    Posted 4 years ago #

    @alchymyth: thank you but it didn't help. The condition is_page('audio') works well because I got displayed what I want on the pages. Only the navigation things are missing ...

    I am really curious if we can make it work .... Hey WordPress gurus :-)

    R

  6. cimenta
    Member
    Posted 4 years ago #

    just tested on clean installation of WordPress + default theme and it works fine. So it must be something about how I implemented in Thesis. Any idea?

    R.

  7. cimenta
    Member
    Posted 4 years ago #

    I made it work by inserting global thing ...

    $temp = $wp_query;  // assign orginal query to temp variable for later use
    global $wp_query;
    global $post;

    Could somebody explain that to me? Why it works with global?

    Thank you

    R.

Topic Closed

This topic has been closed to new replies.

About this Topic