Wordpress and Webdesign Forum go41 » WordPress Code Snippets

  1. I have a custom template listing only posts of one category (category-XX.php)
    Here I am listing only events, future events and passed events. Using the plugin 'MR Event' I get the date of all events easily into a custom field.

    This list got quite long now and I was looking for a code to paginate the custom loop. Paging in a $pageposts = $wpdb->get_results($wp_query->request, OBJECT); loop is not so easy, but I found a solution here: wordpress.org/support/topic/232627

    As this stuff is for advanced users only, I will give you the complete content of my category-XXX.php here.

    To be seen here: http://www.linedancestompers.de/category/events/

    <?php get_header(); ?>
    <!-- cat 'your cat ID' template name category-ID.php -->
    <div id="content">
    <?php if(!is_paged()) { ?>
    <?php	if ( function_exists('mr_event_list_qf') ) { ?>
    <h2>Kommende Termine:</h2>
    <?php mr_event_list_qf('date_format=d.m.Y','%DATE%: <a href="%URL%" title="%TITLE%" >%TITLE%</a>') ?>
    <?php } ?>
    <?php } ?>
    <h2>Alle Termine<?php if ($paged != 1  && $paged) { echo(" Seite $paged" ); } ?>:</h2>
    
    <?php
    $total = "
        SELECT $wpdb->posts.*
        FROM $wpdb->posts
        LEFT JOIN $wpdb->postmeta ON ($wpdb->posts.ID = $wpdb->postmeta.post_id)
        WHERE $wpdb->postmeta.meta_key = 'mr_event'
        AND $wpdb->posts.post_status = 'publish'
        AND $wpdb->posts.post_type = 'post'
        ORDER BY $wpdb->postmeta.meta_value DESC
        ";
    $totalposts = $wpdb->get_results($total, OBJECT); //see wordpress.org/support/topic/232627
    $ppp = intval('12'); //12 posts per page you might use $ppp = intval(get_query_var('posts_per_page'));
    $wp_query->found_posts = count($totalposts);
    $wp_query->max_num_pages = ceil($wp_query->found_posts / $ppp);
    $on_page = intval(get_query_var('paged'));
    if($on_page == 0){ $on_page = 1; }
    $offset = ($on_page-1) * $ppp;
    $wp_query->request = "
        SELECT $wpdb->posts.*
        FROM $wpdb->posts
        LEFT JOIN $wpdb->postmeta ON ($wpdb->posts.ID = $wpdb->postmeta.post_id)
        WHERE $wpdb->postmeta.meta_key = 'mr_event'
        AND $wpdb->posts.post_status = 'publish'
        AND $wpdb->posts.post_type = 'post'
        ORDER BY $wpdb->postmeta.meta_value DESC
        LIMIT $ppp OFFSET $offset";
    $pageposts = $wpdb->get_results($wp_query->request, OBJECT);
     ?>
     <?php if ($pageposts): ?>
      <?php foreach ($pageposts as $post): ?>
        <?php setup_postdata($post); ?>
    <div class="event">
    <ul><li><?php if($mr_event_meta = get_post_meta($post->ID, 'mr_event', true)) { echo date('j.m.Y', $mr_event_meta);echo ' ';echo date('G:i', $mr_event_meta);echo ' Uhr<br />';  } ?></li></ul>
    	<a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title(); ?>"><?php the_title(); ?></a>
    <div class="contenttext">
    <?php the_content() ?>
    </div>
    </div>
     <?php endforeach; ?>
    
    <?php else : ?>
    <p>No matching entries found.</p>
    <?php endif; ?>
    
    <div class="navigation">
    <p><span class="navleft"><?php previous_posts_link('&laquo; Vorherige Seite') ?></span>
    <span class="navright"><?php next_posts_link('N&auml;chste Seite &raquo;') ?></span></p>
    </div>
    
    </div>
    <?php get_sidebar(); ?>
    <?php get_footer(); ?>

    having questions? Join us and ask!

    Posted 1 year ago #
  2. hunsford
    Member

    Do you know if this will work in non-template files? I'm using the above custom query loop in a plugin file, but previous_posts_link() and next_posts_link() don't return anything.

    Posted 1 year ago #
  3. I do not know..

    you might have to put previous_posts_link() and next_posts_link() into the plugin itself.

    If you run other queries on that file WordPress might not 'know' which is the query you want to paginate and the variable for the current page is gone.

    So this xxx_posts_link() has to follow the query you actually want to put into pages directly.

    As you find on top of my query I display the page number if it's paged:

    <?php if ($paged != 1  && $paged) { echo(" Seite $paged" ); } ?>

    I would echo the page number for testing only, in your plugin or where ever you call the plugin, to see if paging is set somehow.
    Like this:

    <?php echo(" this is page $paged" ); ?>

    I am learning more by trial and error than actually knowing how it works ..

    Posted 1 year ago #

RSS feed for this topic

Reply

You must log in to post.

Join us! or log in (lost password?):