WordPress.org

Ready to get started?Download WordPress

Forums

Get post ID using custom field value (1 post)

  1. Renji
    Member
    Posted 2 years ago #

    I am using a function to exclude certain posts from the main site feed using post IDs. It's working by directly inputting the post ID in the function, but when I tried to do the same by using custom fields, it is not working.

    This is the function:

    function exp_post($query) {
    	global $wpdb;
    	$pid = "SELECT * FROM $wpdb->postmeta WHERE meta_key = 'excludefeed'";
    	$posts = $wpdb->get_results($pid);
    	foreach( $posts as $post ) {
    		$exclude[] = $post->ID;
    	}
        if ( $query->is_feed) {
            $query->set('post__not_in', $exclude );
        }
        return $query;
    }
    add_filter( 'pre_get_posts', 'exp_post' );

    What seems to be the problem?

Topic Closed

This topic has been closed to new replies.

About this Topic