WordPress.org

Ready to get started?Download WordPress

Forums

Index or Cat Posts - OrderBy Meta_Key with Page Navigation (2 posts)

  1. NPSites
    Member
    Posted 3 years ago #

    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.php

    Not 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.php

    ALSO 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

    http://codex.wordpress.org/Displaying_Posts_Using_a_Custom_Select_Query#Query_based_on_Custom_Field_and_Sorted_by_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 important

    THANK YOU

    .

  2. vtxyzzy
    Member
    Posted 3 years ago #

    You can use filters to add to a query so that you can use query_posts to get the posts you want. That will let you use the normal WP pagination.

    This code is UNTESTED, but it should be close. Add these functions either to the template or functions.php:

    <?php function mam_posts_fields ($fields) {
       global $mam_global_fields;
       if ($mam_global_fields) $fields .= $mam_global_fields;
       return $fields;
    }
    function mam_posts_join ($join) {
       global $mam_global_join;
       if ($mam_global_join) $join .= $mam_global_join;
       return $join;
    }
    function mam_posts_orderby ($orderby) {
       global $mam_global_orderby;
       if ($mam_global_orderby) $orderby = $mam_global_orderby;
       return $orderby;
    }
    add_filter('posts_fields','mam_posts_fields');
    add_filter('posts_join','mam_posts_join');
    add_filter('posts_orderby','mam_posts_orderby');
    ?>

    Then code your query something like this:

    $mam_global_fields = 'wpmeta.meta_value, if (isnull(wpmeta.meta_value),0,1) as hasdate';
    $mam_global_join = "LEFT JOIN $wpdb->postmeta wpmeta ON
        ({$wpdb->posts}.ID = wpmeta.post_id AND wpmeta.meta_key = 'DateStart')";
    $mam_global_orderby = 'hasdate ASC, wpmeta.meta_value ASC';
    $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    query_posts("paged=$paged");

Topic Closed

This topic has been closed to new replies.

About this Topic