Exclusion of certain articles from the WordPress blog home page

If you are running a blog site built by WordPress and for some reason want to exclude certain articles from the main cycle of the blog’s home page, how should you do that? Today, let’s share relevant methods.

It should be noted that the following methods only apply when the home page is a list of blog posts (main loop). This may not work well for some complex portal or magazine layouts.

article directories

Simply exclude articles by id

If the number of articles you want to exclude is not large, you can do it through the following function, which can be directly added to the functions.php of the topic. Please modify the id array of the articles according to the following tips:

/**
 * Blog home page excludes certain articles
 */
function wpdx_posts_not_in_home( $query ) {
	if ( ! is_admin() && $query->is_main_query() && is_home() ) {

		$exclude_posts = array( 1,2,18 ); //Just modify the article ids in the array as needed, and multiple IDs are separated by English commas
        $query->set( 'post__not_in', $exclude_posts );
		
	}
}
add_action( 'pre_get_posts', 'wpdx_posts_not_in_home', 10, 1 );

we can pre_get_posts Hook to filter the article data on the front page.$query->set()There are many parameters that can be modified, so you can refer to the wp_query document.

More complex ways to exclude articles

Recently, I received a small development project that required to exclude certain articles on the front page of my blog. Moreover, the number of articles was large. Although it can be achieved using the above method, it is really inconvenient to modify the id array every time.

So another method was used to achieve it:Add an option to the article editing interface, check it, and hide the article on the blog home page

Exclusion of certain articles from the WordPress blog home page

Here’s how to implement it.

1. Add setting options to the article editing interface

Add setting options in the article editing interface, using the following code, and add them to the functions.php file of the theme:

/ * *
* add an option to the article editing interface
, /
Function wpdx_add_a_metabox () {
Add_meta_box (
'wpdx_metabox', / / metabox ID
_ _ ('Display option',' wpdx-child'), / / option box title
Callback function of 'wpdx_display_metabox', / / option
'post', / / article type. For multiple article types, please use array array
'normal', / / displayed location (normal, side, advanced)
'default' / / priority (default, low, high, core)
);
}
Add_action ('add_meta_boxes',' wpdx_add_a_metabox')

/ * *
* contents of the option
, /
Function wpdx_display_metabox ($post) {
/ *
* add a security check mechanism
, /
Wp_nonce_field (basename (_ _ FILE__), 'wpdx_metabox_nonce')
/ *
* output option content
, /

$html. = checked (get_post_meta ($post- > ID,'_ wpdx_no_home',true), 'on', false)

$html. = _ ('Don\' t show on the homepage', 'wpdx-child')
Html. ='& lt;/label > & lt;/p >'
/ *
* print all of this
, /
Echo $html
}

/ * *
* Save the value of the option
, /
Function wpdx_save_post_meta ($post_id, $post) {
/ *
* Security check
, /
If (! isset ($_ POST ['wpdx_metabox_nonce'])
|! wp_verify_nonce ($_ POST ['wpdx_metabox_nonce'], basename (_ _ FILE__)) |
Return $post_id
/ *
* check the permissions of the current user role
, /
$post_type = get_post_type_object ($post- > post_type)
If (! current_user_can ($post_type- > cap- > edit_post, $post_id))
Return $post_id
/ *
* do not save this option when saving articles automatically
, /
If (defined ('DOING_AUTOSAVE') & & DOING_AUTOSAVE)
Return $post_id

If ($post- > post_type = = 'post') {/ / define your own post type here
If (! isset ($_ POST ['_ wpdx_no_home']) | | empty ($_ POST ['_ wpdx_no_home']) | | $_ POST ['_ wpdx_no_home']! = = 'on') $_ POST [' _ wpdx_no_home'] = 'off'
Update_post_meta ($post_id,'_ wpdx_no_home', $_ POST ['_ wpdx_no_home'])
}
Return $post_id
}
Add_action ('save_post',' wpdx_save_post_meta', 10, 2)

After adding the code, we can see the effect shown in the above figure.

2. Filter the first article data through hooks

In the code above, we add a custom field _wpdx_no_home, if the option is checked, the value of this field is on, conversely off。For old articles, this field does not exist.

Therefore, we also need to use the pre_get_posts hook to filter the first page article data, which is still the functions.php added to the theme:

/**
 * Home page Excludes articles that contain a certain field
 */
function wpdx_home_pre_get_posts( $query ) {
	if ( ! is_admin() && $query->is_main_query() && is_home() ) {
		
		$meta_query = array(
			'relation' => 'OR',
			//The old article does not exist_wpdx_no_home field
			array(
				'key' => '_wpdx_no_home',
				'value' => '',
				'compare' => 'NOT EXISTS'
			),
			//There is no check option for new articles, and the value of_wpdx_no_home is off
			array(
				'key' => '_wpdx_no_home',
				'value' => 'off',
				'compare' => '=='
			)
		 );
		$query->set( 'meta_query', $meta_query );
	}
}
add_action( 'pre_get_posts', 'wpdx_home_pre_get_posts', 10, 1 );

The above codes are excluded from the front page _wpdx_no_home value is on article.

In this way, as long as the article is checked “Don’t display on the home page”, it can be excluded from the blog list on the home page.

Okay, let’s simply share it here.

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注