WP的即时文章(Wordpress插件)选择性地丢弃来自Feed的帖子

时间:2016-04-18 12:44:49

标签: wordpress wordpress-plugin facebook-instant-articles

我正在尝试自动排除所有没有自定义字段集的文章。我检查了'instant_articles_before_render_post'和'instant_articles_after_render_post'钩子,但我想知道如何使用它们来阻止文章的渲染。有什么想法吗?

2 个答案:

答案 0 :(得分:0)

instant_articles_before_render_postinstant_articles_after_render_post用于在帖子渲染之前/之后启动操作,但无法阻止帖子呈现。您需要做的是挂钩pre_get_posts以更改Facebook Instant Articles使用的主要查询。

如果您查看 facebook-instant-articles.php 插件文件,您将看到以下功能:

function instant_articles_query( $query ) {
    if ( $query->is_main_query() && $query->is_feed( INSTANT_ARTICLES_SLUG ) ) {
        $query->set( 'orderby', 'modified' );
        $query->set( 'posts_per_page', 100 );
        $query->set( 'posts_per_rss', 100 );
        /**
         * If the constant INSTANT_ARTICLES_LIMIT_POSTS is set to true, we will limit the feed
         * to only include posts which are modified within the last 24 hours.
         * Facebook will initially need 100 posts to pass the review, but will only update
         * already imported articles if they are modified within the last 24 hours.
         */
        if ( defined( 'INSTANT_ARTICLES_LIMIT_POSTS' ) && INSTANT_ARTICLES_LIMIT_POSTS ) {
            $query->set( 'date_query', array(
                array(
                    'column' => 'post_modified',
                    'after'  => '1 day ago',
                ),
            ) );
        }
    }
}
add_action( 'pre_get_posts', 'instant_articles_query', 10, 1 );

你可以在此之后挂钩并添加你自己的元条件:

function instant_articles_query_modified($query) {
    if($query->is_main_query() && isset(INSTANT_ARTICLES_SLUG) && $query->is_feed(INSTANT_ARTICLES_SLUG)) {
        $query->set('meta_query', array(
            array(
                  'key' => 'your_required_meta'
            )
        ));
}
add_action('pre_get_posts', 'instant_articles_query_modified', 10, 2);

答案 1 :(得分:0)

感谢。上面的代码不能正常工作,因为它缺少一个结束}而且isset导致了一个问题。

试试这个:

    function instant_articles_query_modified($query) {
        if($query->is_main_query() && null!==INSTANT_ARTICLES_SLUG && $query->is_feed(INSTANT_ARTICLES_SLUG)) {
            $query->set('meta_query', array(
                array(
                      'key' => 'your_required_meta'
                )
            ));    
        }
    }