• Hi folks,

    I’m struggling with a piece of code that a helpful guy in the IRC channel gave me that he said “should” work. I appreciate the time he took to put it together but alas it didn’t work as expected.

    Here it is:

    function my_filter_query($query) {
    	if( !is_feed() )
    		return;
    	$query->query_vars['meta_key'] = '_rss_include';
    	$query->query_vars['meta_value'] = '0';
    	$query->query_vars['meta_compare'] = '!=';
    }
    add_action('pre_get_posts','my_filter_query');

    The whole idea is that I have a custom post type (I don’t know if this matters) for upcoming gigs (website for a concert venue) but not every gig requires a post page with information on it, just an inclusion in the listings.

    From that point I decided I would remove any permalinks to these posts and make sure they don’t appear in the feed so any subscribers don’t end up with links to empty posts containing just a band name.

    Can anybody tell me what’s wrong with the code above? I’ve included it in my theme’s functions.php and my theme is a child of TwentyTen.

    If somebody can point me in the right direction that’d be magic 😀

    Cheers,
    Robert

Viewing 5 replies - 1 through 5 (of 5 total)
  • Note sure since I haven’t touched the hooks API. But based on the examples at http://codex.wordpress.org/Custom_Queries, it seems like you have to return the query object.

    Thread Starter RSimpson

    (@rsimpson)

    I gave it a try, added it in right after $query->query_vars[‘meta_compare’] = ‘!=’;

    No luck yet 🙁

    Moderator Samuel Wood (Otto)

    (@otto42)

    WordPress.org Admin

    Try putting global $query; at the beginning of your function.

    Thread Starter RSimpson

    (@rsimpson)

    Tried it, no change 🙁

    Could the function I’ve added to include posts from the custom post type ‘gigs’ interfere with it?

    Here’s my current code:

    /* Add gig posts to RSS feed */
    function myfeed_request($qv) {
    	if (isset($qv['feed']) && !isset($qv['post_type']))
    		$qv['post_type'] = array('post', 'gigs');
    	return $qv;
    }
    add_filter('request', 'myfeed_request');
    
    /* Filter out unwanted posts from the RSS feed. */
    function my_filter_query($query) {
    	global $query;
    	if( !is_feed() )
    		return;
    	$query->query_vars['meta_key'] = '_rss_include';
    	$query->query_vars['meta_value'] = '0';
    	$query->query_vars['meta_compare'] = '!=';
    	return $query;
    }
    add_action('pre_get_posts','my_filter_query');

    Cheers,
    Robert

    global $query is a bad idea, a $query variable is already passed to the function however if there was another variable in the global space named $query, you might end up working on the wrong variable even if you got the rest of the code fixed.

    I’d do a brute force debug just var_dump($query); die(); to see what is the value of $query when it reaches my_filter_query.

    Also, if your other function might be causing interference, why not lump the two together into a single function to add in gigs while filtering out those without rss include set?

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘exclude post from RSS feed using custom field’ is closed to new replies.