Index or Cat Posts – OrderBy Meta_Key with Page Navigation
-
Hey I have site that shows events
I need to sort them and display future events .. I don’t really mind if older ones are shown also but eventually I will want to only show posts/events that are within the next 60 days (i have a metakey with 00/00/00 dates)
I thought I could get away with using the slug or post title to sort and each has a value like this
Post title
08/02/2010 – Big Horn County Fair – Basin, Wyoming
Post Slug
08022010-big-horn-county-fair-basin-wyoming.phpNot having a great time trying to orderby title or slug(name) because the ID of the post is placed before the slug in the permalink structure to guard against duplicates…
site.com/2016/08022010-big-horn-county-fair-basin-wyoming.phpALSO I would like to be able to add more functionality so I started looking at the MetaKey as a sort value.
I have a meta_key called DateStart for the day an event is happening and one called DateEnd for the day it ends but DateEnd is not always populated…
My first attempt was something like this
Not every post on my site is an event so about half dont have DateStart custom fields… this could be a problem so I am checking for them<?php $values = get_post_meta(get_the_ID(), 'DateStart'); foreach ( $values as $DateStart ) if ($DateStart !== "") { query_posts('meta_key=DateStart&orderby=meta_value&order=DESC'); } ?>
Then I found some code to sort by MetaKey Value
This seems to work somewhat (meaning it sorts) but it returns all posts
So I add the LIMIT 10 to limit the number of post retrieved but there is no way to add page navigation on index.php or in a Category because the information is just not there unless I do another query and a count.
MY QUESTION FOR NOW IS
How do I do a complex sort by meta_value and also allow for post navigation so visitors can continue reading older posts?The code i have now is
<?php $querystr = " SELECT wposts.* FROM $wpdb->posts wposts, $wpdb->postmeta wpostmeta WHERE wposts.ID = wpostmeta.post_id AND wpostmeta.meta_key = 'DateStart' AND wposts.post_status = 'publish' AND wposts.post_type = 'post' ORDER BY wpostmeta.meta_value DESC LIMIT 10 "; $pageposts = $wpdb->get_results($querystr, OBJECT); ?> <?php if ($pageposts): ?> <?php global $post; ?> <?php foreach ($pageposts as $post): ?> <?php setup_postdata($post); ?>
Like i said above not all my posts have the metakey DateStart because they are not all events… Most of those posts are in separate categories so category templates may help for event and non event categories to display the posts sorted by DateStart or postdate if not an event… not sure how that will get mixed together on the index or a parent cat that has both types though…
Many Questions..
If you have any answers …
right now I guess navigation links is most importantTHANK YOU
.
- The topic ‘Index or Cat Posts – OrderBy Meta_Key with Page Navigation’ is closed to new replies.