• I have a list of posts that I want to allow the user to filter based on information in custom fields. Both $iTimeframe and $iRegion are user inputs from dropdowns. If they have a value then the query needs to be updated. This is being done with the add_filter to the post_where. The first one for $iTimeframe works as expected, however the one for $iRegion–where it is necessary to actually pass the string for the region in order to get a match–doesn’t work.

    $args = array(
        'offset' => $setOffset,
        'category_name' => $pdOutput[0],
        'orderby' => 'meta_value',
        'meta_key' => 'EventDate',
        'order' => 'ASC',
        'paged' => $paged,
        'posts_per_page' => LIMIT_PD
        );
    
        if ($iTimeframe == "upcoming") {
          add_filter( 'posts_where', 'filter_where_pd_upcoming' );
        } else {
          add_filter( 'posts_where', 'filter_where_pd_past' );
        } 
    
        if ($iRegion !="") {
          add_filter( 'posts_where','filter_where_pd_region', '', 1 );
        } 
    
        $pdlist_query = new WP_Query( $args );
        [and Loop...]
    
    function filter_where_pd_upcoming () {
        $where .= " AND meta_key = 'EventDate' AND FROM_DAYS(TO_DAYS(meta_value))+0 >= CURDATE()";
        return $where;
    }
    function filter_where_pd_past () {
        $where .= " AND meta_key = 'EventDate' AND FROM_DAYS(TO_DAYS(meta_value))+0 < CURDATE()";
        return $where;
    }
    function filter_where_pd_region ($iRegion) {
        $where .= " AND meta_key = 'RegionBC' AND meta_value='".$iRegion."'";
        return $where;
    }

    Based on the tests I’ve done, I don’t think $iRegion is making it inside the function. When I tried echoing $iRegion inside the function I got the following:

    AND ( wp_term_relationships.term_taxonomy_id IN (24) ) AND wp_posts.post_type = 'post' AND (wp_posts.post_status = 'publish' OR wp_posts.post_status = 'private') AND wp_postmeta.meta_key = 'EventDate'

    …which I’m guessing is actually the where statement being picked up.

    Help would be greatly appreciated.

  • The topic ‘trying to pass a variable into an add_filter’ is closed to new replies.