WP 即时文章(Wordpress 插件)有选择地丢弃提要中的帖子
Instant Articles for WP (Wordpress plugin) Selectively discard posts from feed
我正在尝试自动排除所有未设置自定义字段的文章。我已经检查了 'instant_articles_before_render_post' 和 'instant_articles_after_render_post' 挂钩,但我想知道如何使用它们来防止文章呈现。有什么想法吗?
instant_articles_before_render_post
和 instant_articles_after_render_post
用于在 post 渲染之前/之后启动动作,但不能阻止 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);
谢谢。上面的代码不太有效,因为它缺少结束符 } 并且 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'
)
));
}
}
我正在尝试自动排除所有未设置自定义字段的文章。我已经检查了 'instant_articles_before_render_post' 和 'instant_articles_after_render_post' 挂钩,但我想知道如何使用它们来防止文章呈现。有什么想法吗?
instant_articles_before_render_post
和 instant_articles_after_render_post
用于在 post 渲染之前/之后启动动作,但不能阻止 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);
谢谢。上面的代码不太有效,因为它缺少结束符 } 并且 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'
)
));
}
}