2016-04-18 10 views
1

カスタムフィールドセットを持たない記事をすべて自動的に除外しようとしています。私は 'instant_articles_before_render_post'と 'instant_articles_after_render_post'フックをチェックしましたが、記事のレンダリングを防ぐためにそれらをどのように使うことができるのだろうと思います。何か案は?WP(Wordpress plugin)のインスタント記事フィードからの投稿を選択的に破棄

答えて

0

instant_articles_before_render_postおよびinstant_articles_after_render_postは、ポストレンダリングの前後にアクションを開始するために使用されますが、ポストがレンダリングされるのを防ぐことはできません。あなたがする必要があるのは、Facebook Instant Articlesで使用されるメインクエリを変更するためにpre_get_postsにフックすることです。あなたはFacebookの-インスタント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); 
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' 
       ) 
      ));  
     } 
    } 
関連する問題