• I am modifying an existing theme. It has events listing functionality using a post category (not custom post type). Originally it was just listing all of the events, but it needed to show upcoming events (so people see the next event first). I got this to work perfectly by adding the following query to the template events.php:
    ‘$is_events_child = cat_is_ancestor_of($_reserved_categories[‘events’], get_query_var(‘cat’));

    if( is_category(‘events’))
    {query_posts($query_string . ‘&meta_key=fw_event_time&meta_value=’.time().’&meta_compare=>&orderby=meta_value&order=ASC’);
    }
    if($is_events_child)
    {query_posts($query_string . ‘&meta_key=fw_event_time&meta_value=’.time().’&meta_compare=>&orderby=meta_value&order=ASC’);
    } /*there’s prob a better way to do this but this worked to include events sub-categories*/’

    I want to add a link at the top that says “View past events”. I think the simplest way to do this is to run a new query and change meta_compare to =< . So I added this:
    “>View past events

    $past_events = query_posts($query_string . ‘&meta_key=fw_event_time&meta_value=’.time().’&meta_compare=<&orderby=meta_value&order=ASC’);’

    This doesn’t work – any suggestions? I am fairly new to php so I’m probably doing something stupid…

Viewing 3 replies - 1 through 3 (of 3 total)
  • Andrew Nevins

    (@anevins)

    WCLDN 2018 Contributor | Volunteer support

    I’m obliged to recommend you create a child-theme instead of modifying the current theme.

    Thread Starter banannah

    (@banannah)

    sorry, my code did not seem to work – let’s try again:

    Original that shows only upcoming events:

    $is_events_child = cat_is_ancestor_of($_reserved_categories['events'], get_query_var('cat'));
    
    if( is_category('events'))
    {query_posts($query_string . '&meta_key=fw_event_time&meta_value='.time().'&meta_compare=>&orderby=meta_value&order=ASC');
    }
    if($is_events_child)
    {query_posts($query_string . '&meta_key=fw_event_time&meta_value='.time().'&meta_compare=>&orderby=meta_value&order=ASC');
    } /*there's prob a better way to do this but this worked to include events sub-categories*/

    What I added to try and display only past events:

    <a href="<?php echo $past_events; ?>">View past events</a>
    
    $past_events = query_posts($query_string . '&meta_key=fw_event_time&meta_value='.time().'&meta_compare=<&orderby=meta_value&order=ASC');

    Thread Starter banannah

    (@banannah)

    @anevins heh, yes that would be better, but as the ‘events’ functionality needed to be modified and restructured quite a bit I just decided to modify the existing one – I was having trouble making some changes work using a child theme. But thanks, you’re completely right ??

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘query posts by custom field value by clicking on a link?’ is closed to new replies.