whyisjake / Simple-Instant-Articles-for-Facebook

Add support for Facebook Instant Articles to your WordPress site.
https://wordpress.org/plugins/simple-facebook-instant-articles/
55 stars 19 forks source link

Selecting individual posts for FB instant articles #59

Closed sarpysarpy closed 8 years ago

sarpysarpy commented 8 years ago

Hi, would it be possible to include a custom field in Wordpress editing page to select which posts gets into fb instant articles instead of all of them?

Thank you.

whyisjake commented 8 years ago

Yeah, totally. We are doing this at WIRED. Here is some sample code that you could use:


add_action( 'pre_get_posts', 'wired_fb_instant_article_customize_feed_query' );

/**
 * Set WP query variables for FB IA feed, so we can customize
 * what posts are considered for the feed.
 *
 * Was once pre_get_posts
 *
 * @param $query WP_Query object.
 */
function wired_fb_instant_article_customize_feed_query( $query ) {

    if ( $query->is_main_query() && $query->is_feed( 'fb' )  ) {

        //Get original meta query
        $meta_query = $query->get( 'meta_query' );

        //Add our meta query to the original meta queries
        $meta_query[] = array(
            'key'      => 'facebook_instant_feature',
            'value'    => 1,
            'compare'  => '=',

        );

        $query->set( 'meta_query', $meta_query );
    }
}