Wordpress, filter messages on main page

2019-08-29 07:18发布

How can I hide posts from some category with some ID from main page of my site? I need solution like filter:

function exclude_post($query) {
    if ($query->is_home) {
        ...
    }

    return $query;
}

add_filter('pre_get_posts', 'exclude_post');

Can somebody provide an example?

标签: php wordpress
3条回答
Lonely孤独者°
2楼-- · 2019-08-29 07:29

Use $query->set( $query_var, $value ); where $query_var is the variable you want to add/update in query. So put this inside your condition:

// 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 ) );

Remember is good practice put in condition a check to $query->is_main_query(). pre_get_posts is an action hook so you have to change add_filter to add_action. An action hook doesn't return a value, a filter does.

Example

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');

UPDATE

According to the new details emerging by the question,in order to exclude some posts in feeds stream, but not in category archive, the conditional check might look like:

if( $query->is_feed() && !$query->is_archive() )

OR

if( $query->is_feed() && !$query->is_category() )

Hope it helps!

查看更多
Lonely孤独者°
3楼-- · 2019-08-29 07:30

you can also use the following way to exclude the a category from post query

 <?php 
 if ( is_home() ) 
 {
 query_posts($query_string . '&cat=-3');
 }
 ?>
查看更多
Fickle 薄情
4楼-- · 2019-08-29 07:42

You can use simple exclude in the query parameters :-

<?php wp_list_categories('orderby=name&show_count=1&exclude=10'); ?> 

Its only sample , how to you exclude.

Hop it work..

查看更多
登录 后发表回答