修改Hooker逻辑:Wordpress和即时文章

时间:2016-08-24 23:40:10

标签: php wordpress facebook

以下代码在Automattic的Facebook Instant Articles插件中运行。这是用于生成facebook为即时文章提取的RSS提要的查询。

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 );
        $settings_categories = Instant_Articles_Option_Categories::get_option_decoded();
        if($settings_categories['categories'] !== '') {
            $query->set( 'cat', $settings_categories['categories'] );
        }
        /**
         * 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 mod_ia_query( $query ) {

   $query->set( 'meta_query', array(
    array(
          'key' => 'is_instant_article',
          'value' => true
    ),
   );


}
add_action('instant_articles_query','mod_ia_query', 10, 1); 

1 个答案:

答案 0 :(得分:0)

我回来更新这个,但基本上我所做的是在我的主题中创建一个子目录,其中包含facebook instant articles插件的名称,并创建一个扩展Instant_Articles类的类。

我取消了add_action( 'pre_get_posts', 'instant_articles_query', 10, 1 );

并将自己的方法添加到钩子中,这在插件的上下文中执行我的功能,但不直接编辑核心插件文件。