WordPress.org

Ready to get started?Download WordPress

Forums

query posts by custom field value by clicking on a link? (4 posts)

  1. banannah
    Member
    Posted 1 year ago #

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

  2. Andrew Nevins
    Barrel Rider, Spam Zapper & Volunteer Moderator
    Posted 1 year ago #

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

  3. banannah
    Member
    Posted 1 year ago #

    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');
  4. banannah
    Member
    Posted 1 year ago #

    @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 :)

Topic Closed

This topic has been closed to new replies.

About this Topic