How to customize the Filter search on the Posts Lists(edit.php)

This customize is how to simply Filter search on the Posts list.

 

Customize for Filter search

Customize for Filter search

 

 

Step1 Add hook to the only Posts list.

 

Please add below code to functions.php of your theme.

add_action( 'load-edit.php' , 'custom_load_edit' );

function custom_load_edit() {
	
}

 

This will continue to perform the customize in it custom_load_edit function.

 

 

Step2 Remove the All Dates select box.

 

All dates filter search is not very necessary. So it will remove.

 

All Dates

All Dates

 

Please add code to custom_load_edit function.

add_filter( 'disable_months_dropdown' , 'custom_disable_months_dropdown' , 10 , 2 );

 

And please add code to functions.php

function custom_disable_months_dropdown( $false , $post_type ) {
	
	$disable_months_dropdown = $false;
	
	$disable_post_types = array( 'post' , 'page' );
	
	if( in_array( $post_type , $disable_post_types ) ) {
		
		$disable_months_dropdown = true;
		
	}
	
	return $disable_months_dropdown;
	
}

 

 

It looks like this.

 

Remove the All Dates

Remove the All Dates

 

 

Step2 Remove the All categories.

 

All categories filter does not have disable filter like the All dates. So it need to hide in the CSS.

Please add code to custom_load_edit function.

add_action( 'admin_print_styles' , 'custom_admin_print_styles' );

 

And please add code to functions.php.

function custom_admin_print_styles() {
	
	echo '<style>';
	echo '#posts-filter #cat{ display: none; }';
	echo '</style>';
	
}

 

It looks like this.

 

Remove the All Categories

Remove the All Categories

 

 

Step3 Remove the Filter search button.

 

Filter search button also need to hidden in the CSS.

Please add code to under the #posts-filter #cat of custom_admin_print_styles function.

echo '#posts-filter #post-query-submit{ display: none; }';

 

It looks like this.

 

Remove the Filter search button

Remove the Filter search button

 

This is complete! 🙂

 

 

If you use to WordPress SEO plugin(WordPress SEO by Yoast).

 

Show the select box of All SEO scores filter automatic when activate WordPress SEO plugin.

 

All SEO Scores

All SEO Scores

 

This will hide to it item.

Please add code to custom_load_edit function.

 

global $wpseo_metabox;
	
if( !empty( $wpseo_metabox ) ) {

	remove_action( 'restrict_manage_posts' , array( $wpseo_metabox , 'posts_filter_dropdown' ) );
		
}

 

 

If you want show the custom fields filter.

 

Try to Filter to look for the same price.

Please add code to custom_load_edit function.

 

add_action( 'restrict_manage_posts' , 'custom_restrict_manage_posts' );

 

And please add code to functions.php.

function custom_restrict_manage_posts() {

	global $wpdb;
	global $post_type;
	
	$post_ids = $wpdb->get_col( $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE post_type = %s", $post_type ) );

	if( empty( $post_ids ) ) {
		
		return false;
		
	}

	$query = 'SELECT DISTINCT meta_value ';
	$query .= "FROM $wpdb->postmeta ";
	$query .= "WHERE meta_key = 'price' ";
	$query .= 'AND post_id IN( ' . implode( ',' , $post_ids ) . ') ';
	$query .= 'ORDER BY meta_value+0 ASC ';
	
	$custom_fields = $wpdb->get_results( $query );
	
	if( empty( $custom_fields ) ) {
		
		return false;
		
	}
	
?>

	<label for="filter-by-price" class="screen-reader-text"><?php _e( 'Price' ); ?></label>
	<select name="meta_price" id="filter-by-price">
	
		<option value=""><?php _e( 'All Prices' ); ?></option>
	
		<?php foreach( $custom_fields as $custom_field ) : ?>
		
			<option value="<?php echo esc_attr( $custom_field->meta_value ); ?>"><?php echo esc_attr( $custom_field->meta_value ); ?></option>
		
		<?php endforeach; ?>
	
	</select>

<?php

}

 

 

It looks like this.

 

Add Prices Filter

Add Prices Filter

 

It is looks complete. However this Filter does not working yet.

So please add code to custom_load_edit function.

 

add_filter( 'request', 'custom_request_query' );

 

And please add code to functions.php.

function custom_request_query( $vars ) {
	
	global $pagenow;
	global $post_type;
	
	$possible_post_types = array( 'post' , 'page' );

	if( !empty( $pagenow ) && $pagenow == 'edit.php' && in_array( $post_type , $possible_post_types ) ) {
		
		if ( ! empty( $_GET['meta_price'] ) ) {
			
			$meta_price = intval( $_GET['meta_price'] );
			
			$vars['meta_key'] = 'price';
			$vars['meta_value'] = $meta_price;

		}

	}
	
	return $vars;

}

 

It looks like this.

 

Filter search for Custom FIelds

Filter search for Custom FIelds

 

 

Full code is here.

 

add_action( 'load-edit.php' , 'custom_load_edit' );

function custom_load_edit() {
	
	global $wpseo_metabox;

	add_filter( 'disable_months_dropdown' , 'custom_disable_months_dropdown' , 10 , 2 );
	add_action( 'admin_print_styles' , 'custom_admin_print_styles' );

	add_action( 'restrict_manage_posts' , 'custom_restrict_manage_posts' );
	add_filter( 'request', 'custom_request_query' );
	
	if( !empty( $wpseo_metabox ) ) {

		remove_action( 'restrict_manage_posts' , array( $wpseo_metabox , 'posts_filter_dropdown' ) );
		
	}
	
}

function custom_disable_months_dropdown( $false , $post_type ) {
	
	$disable_months_dropdown = $false;
	
	$disable_post_types = array( 'post' , 'page' );
	
	if( in_array( $post_type , $disable_post_types ) ) {
		
		$disable_months_dropdown = true;
		
	}
	
	return $disable_months_dropdown;
	
}

function custom_admin_print_styles() {
	
	echo '<style>';
	echo '#posts-filter #cat{ display: none; }';
	//echo '#posts-filter #post-query-submit{ display: none; }';
	echo '</style>';
	
}

function custom_restrict_manage_posts() {

	global $wpdb;
	global $post_type;
	
	$post_ids = $wpdb->get_col( $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE post_type = %s", $post_type ) );

	if( empty( $post_ids ) ) {
		
		return false;
		
	}

	$query = 'SELECT DISTINCT meta_value ';
	$query .= "FROM $wpdb->postmeta ";
	$query .= "WHERE meta_key = 'price' ";
	$query .= 'AND post_id IN( ' . implode( ',' , $post_ids ) . ') ';
	$query .= 'ORDER BY meta_value+0 ASC ';
	
	$custom_fields = $wpdb->get_results( $query );
	
	if( empty( $custom_fields ) ) {
		
		return false;
		
	}
	
?>

	<label for="filter-by-price" class="screen-reader-text"><?php _e( 'Price' ); ?></label>
	<select name="meta_price" id="filter-by-price">
	
		<option value=""><?php _e( 'All Prices' ); ?></option>
	
		<?php foreach( $custom_fields as $custom_field ) : ?>
		
			<option value="<?php echo esc_attr( $custom_field->meta_value ); ?>"><?php echo esc_attr( $custom_field->meta_value ); ?></option>
		
		<?php endforeach; ?>
	
	</select>

<?php

}

function custom_request_query( $vars ) {
	
	global $pagenow;
	global $post_type;
	
	$possible_post_types = array( 'post' , 'page' );

	if( !empty( $pagenow ) && $pagenow == 'edit.php' && in_array( $post_type , $possible_post_types ) ) {
		
		if ( ! empty( $_GET['meta_price'] ) ) {
			
			$meta_price = intval( $_GET['meta_price'] );
			
			$vars['meta_key'] = 'price';
			$vars['meta_value'] = $meta_price;

		}

	}
	
	return $vars;

}