1

I can have the RSS feed url like:

http://www.mydomain.com/?post_type=job_listing&job_cat=value#1&job_type=value#2&geo_country=value#3&feed=rss2

job_cat & job_type are taxonomies for the job_listing post type and they are considered / generated in the RSS feeds correctly.

Recently I've added a custom field parameter in te RSS feed url's called geo_country. If a value is returned for this in the RSS feed url (not empty), then the RSS content should be filtered also by the custom field's value. Which is not happening currently, of course.

Using the Jobbroller theme, but I haven't found in them any RSS related code, function(s).

Is there any way to write a hook in the described scenario?

2
  • What kind of values does geo_country take?
    – birgire
    Oct 14, 2013 at 13:48
  • Only alpa-numeric values :)
    – user17408
    Oct 14, 2013 at 19:14

1 Answer 1

2
+150

You can try to add geo_country as an extra query variable with:

/**
 * Add the 'geo_country' as a public query variable
 *
 * @param array $query_vars
 * @return array $query_vars
 */ 
function my_query_vars( $query_vars ) 
{
    $query_vars[] = 'geo_country';
    return $query_vars;
}

add_filter( 'query_vars', 'my_query_vars' );

and then setup a pre_get_posts hook to filter your feed according to the geo_country value:

/**
 * Filter the feed by the 'geo_country' meta key 
 *
 * @param WP_Query object $query
 * @return void 
 */ 
function my_pre_get_posts( $query ) 
{
    // only for feeds
    if( $query->is_feed && $query->is_main_query() ) 
    {
        // check if the geo_country variable is set 
        if( isset( $query->query_vars['geo_country'] ) 
                && ! empty( $query->query_vars['geo_country'] ) )
        {

            // if you only want to allow 'alpha-numerics':
            $geo_country =  preg_replace( "/[^a-zA-Z0-9]/", "", $query->query_vars['geo_country'] ); 

            // set up the meta query for geo_country
            $query->set( 'meta_key', 'geo_country' );
            $query->set( 'meta_value', $geo_country );
        }

    } 
}

add_action( 'pre_get_posts', 'my_pre_get_posts' );

I assume geo_country takes only alpha-numeric values (a-z,A-Z,0-9), just let me know if that's not the case.

This works on my install with the Twenty Twelve theme.

9
  • Thanks you for your help! It seems to work like a charm! Wonderful. So first a public query variable must be built and then filter the pre_get_posts. Admirable :)
    – user17408
    Oct 14, 2013 at 19:13
  • It says that I can award the bounty in 12 hours. Please remind me kindly if I forget to award you in max 24 hours.
    – user17408
    Oct 14, 2013 at 19:14
  • Thanks, I'm happy to hear it solves your problem, cheers.
    – birgire
    Oct 14, 2013 at 19:26
  • One tiny little thing. It isn't your fault of course, but it isn't working for countries that are 2 words (I.e. United Kingdom or United States). Perhaps something to so with the space? What it is recommended on such cases?
    – user17408
    Oct 15, 2013 at 7:34
  • In general I would recommend using lower case initials for the geo_country url parameter, like uk, us, fr, ... etc if that's possible for you. Otherwise you should check the values with var_dump( $query->query_vars['geo_country'] ); before the preg_replace to figure out what other characters to allow (other than a-zA-Z0-9).
    – birgire
    Oct 15, 2013 at 8:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.