Ordering posts by custom field values (EVENT LIST) Pagination? (2 posts)

  1. sweeneydavidf
    Posted 4 years ago #

    Okay so I have been beating the bushes for days now trying to find a solution to my problem. I need to order posts on my events page by a custom field I added called "event_date". I use the 2011-05-05 format in the field and I finally got some code working that actually orders them the way I want.

    The only problem is that I had to remove the previous code that also included the post_per_page limit.

    Here is my code:

    	$querystr = "
    	    SELECT wposts.*
    	    FROM $wpdb->posts wposts, $wpdb->postmeta wpostmeta
    	    WHERE wposts.ID = wpostmeta.post_id
    	    AND wpostmeta.meta_key = 'event_date'
    	    AND wposts.post_type = 'post'
    	    ORDER BY wpostmeta.meta_value ASC
    	 $pageposts = $wpdb->get_results($querystr, OBJECT);
    	 <?php if ($pageposts): ?>
    	  <?php foreach ($pageposts as $post): ?>
    	    <?php setup_postdata($post); ?>
    		<div class="blog_post shadow_round">
    			<div <?php post_class() ?> id="post-<?php the_ID(); ?>">
    				<h3 class="post_title"><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></h3>
    				<div class="post_date"><?php the_time(get_option('date_format')); ?></div>
    				<hr />
    				<div class="post_blurb">
    					<?php the_content(); ?>
    				<?php include ('sharethis.php' ); ?>
    		<?php endforeach; ?>
    	<?php include (TEMPLATEPATH . '/inc/nav.php' ); ?>
    	<?php else : ?>
    		<h2>Not Found</h2>
    	<?php endif; ?>

    Does anyone have a solution? I don't even really need pagination, I would just settle for limiting the number of posts that display because I not only need this on the Events page but I am also putting the event posts into the sidebar where I only want to show 2 upcoming events at most.

    Its amazing to me how awesome custom fields can be, but there is no way to easily order posts using them.

    Thanks alot in advance.

  2. sweeneydavidf
    Posted 4 years ago #

    Up for the morning. Thanks.

Topic Closed

This topic has been closed to new replies.

About this Topic