WordPress.org

Ready to get started?Download WordPress

Forums

Getting navigation to work when using get_posts (4 posts)

  1. sosa3432
    Member
    Posted 2 years ago #

    I'm a real beginner at this so bear with me if this is really basic. I've looked around but can't seem to find a good answer.

    I'm running a custom loop for the plugin Event Organiser, using its eo_get_events() which is analogous to WordPress’ built-in get_posts().

    I've managed to get the loop to run and display the events, but the navigation isn't showing up (older/newer posts). I think that this has something to do with the wp_query and that I somehow have to "hijack" it, but I havent managed to find any code that works with get_posts, only with other versions of the loop.

    Anyone have any ideas? Thanks!

  2. Stephen Harris
    Member
    Posted 2 years ago #

    That's right, get_posts is the wrong thing to use for the default navigation - you want to WP_Query. This excepts exactly the same arguments as get_posts (and eo_get_events) but you will need to explicitly set post_type to 'event'.

    If you want to know about adapting the main query see this Codex page or try WordPress Stack Exchange.

  3. sosa3432
    Member
    Posted 2 years ago #

    Thanks again for your quick answer Stephen :)

    I'm trying this, but it only brings back regular posts no matter what I put after post_type. I've tried "event" and "draft", but it always just gives the regular posts. Any ideas?

    <?php
    $the_query = new WP_Query( 'post_type=event' );
    $the_query->query('posts_per_page=-1'.'&paged='.$paged);
    ?>
    <?php while ($the_query->have_posts()) : $the_query->the_post(); ?>
    	<li><a href="<?php the_permalink() ?>" rel="bookmark"><?php the_title(); ?></a></li>
    <?php endwhile; ?>
    <?php wp_reset_postdata();?>
  4. Stephen Harris
    Member
    Posted 2 years ago #

    You second line is a second query which over-rides the first, (and post type isn't set). Also, if you want pagination, then you don't want to show all posts on the first page, so posts_per_page should something other than -1.

    Try:

    $paged = get_query_var( 'paged' ) ? get_query_var( 'paged' ) : 1;
         $the_query = new WP_Query( array(
            'post_type'=>'event',
            'posts_per_page'=> 10,
            'paged'=> $paged
          ));

    And omit the line
    $the_query->query('posts_per_page=-1'.'&paged='.$paged);

    Draft is a post status, not type by the way. If you have any more questions about this, please use WordPress Stack Exchange as this isn't really to do with Event Organiser :D.

Topic Closed

This topic has been closed to new replies.

About this Topic