Support » Fixing WordPress » Exclude posts that have a specific meta_key

  • I’m trying to set up two RSS feeds, one for articles, one for links. The link posts have a custom field that results in a meta_key called “linked_list_url” (I’m using the Daring Fireball Linked List plugin). I have a custom feed set up for the link posts that searches for posts that have a meta_key called “linked_list_url”.

    Where I’m running into problems is for the rest of the posts…basically any post that does not have a meta_key set to linked_list_url.

    Here’s what I have for the linked posts feed:

    $posts = query_posts( array(
    	'showposts' => .$numposts.
    	'meta_key' => 'linked_list_url&orderby=post_id&order=DESC' )); // replace the number 1 with the ID of your tumblelog category
    $more = 1;

    I tried this for the non-link post feed:

    // add a filter to 'posts_where' to add the subquery
    add_filter( 'posts_where', '_exclude_meta_key_in_posts_where' );    
    
    // make the query, the below function will be called
    query_posts(array('showposts' => .$numposts., 'post_parent' => $post->ID, 'post_type' => 'post', 'orderby' => 'date', 'order' => 'DESC'));
    
    function _exclude_meta_key_in_posts_where( $where ) {
    
        global $wpdb;
        return $where . " AND $wpdb->posts.ID NOT IN ( SELECT DISTINCT post_id FROM $wpdb->postmeta WHERE meta_key = 'linked_list_url' )";
    }
    
    //remove the filter incase we do any more query_posts()s
    remove_filter( 'posts_where', '_exclude_meta_key_in_posts_where' );

    The linked list feed works like a charm, but I can’t get the second one to work. Does anyone have any ideas?

  • The topic ‘Exclude posts that have a specific meta_key’ is closed to new replies.