サイトのメイン ページから特定の ID を持つ特定のカテゴリの投稿を非表示にするにはどうすればよいですか? フィルターのようなソリューションが必要です:
function exclude_post($query) {
if ($query->is_home) {
...
}
return $query;
}
add_filter('pre_get_posts', 'exclude_post');
誰かが例を提供できますか?
$ query_var$query->set( $query_var, $value );
は、クエリで追加/更新する変数です。だからあなたの条件の中にこれを入れてください:
// 1st parameter is the query variable the 2nd is its value, in this case an array of category IDs
$query->set( 'category__not_in', array( 2, 6 ) );
にチェックを入れることをお勧めし$query->is_main_query()
ます。pre_get_posts
はアクション フックなので、に変更add_filter
する必要がありますadd_action
。アクション フックは値を返しませんが、フィルターは値を返します。
例
function exclude_post( $query ) {
if ( $query->is_home() && $query->is_main_query() ) {
$query->set( 'category__not_in', array( 2, 6 ) );
}
}
add_action('pre_get_posts', 'exclude_post');
アップデート
質問によって明らかになった新しい詳細によると、カテゴリ アーカイブではなく、フィード ストリーム内のいくつかの投稿を除外するために、条件付きチェックは次のようになります。
if( $query->is_feed() && !$query->is_archive() )
また
if( $query->is_feed() && !$query->is_category() )
それが役に立てば幸い!