Skip to content

Instantly share code, notes, and snippets.

@dharma017
Last active October 22, 2016 06:36
Show Gist options
  • Save dharma017/e9b0bbd7126e302f8ac1cdfe544f460b to your computer and use it in GitHub Desktop.
Save dharma017/e9b0bbd7126e302f8ac1cdfe544f460b to your computer and use it in GitHub Desktop.
Wordpress Admin Filter BY Custom Fields
<?php
/*
Plugin Name: Room Filter BY Resort Custom Fields
Plugin URI: http://en.bainternet.info
Description: answer to http://wordpress.stackexchange.com/q/45436/2487
Version: 1.0
Author: Bainternet
Author URI: http://en.bainternet.info
*/
add_action( 'restrict_manage_posts', 'room_admin_posts_filter_restrict_manage_posts' );
/**
* First create the dropdown
* make sure to change POST_TYPE to the name of your custom post type
*
* @author Ohad Raz
*
* @return void
*/
function room_admin_posts_filter_restrict_manage_posts(){
$type = 'post';
if (isset($_GET['post_type'])) {
$type = $_GET['post_type'];
}
//only add filter to post type you want
if ('room' == $type){
//change this to the list of values you want to show
//in 'label' => 'value' format
$values = array();
global $wpdb;
$posts = $wpdb->get_col("
SELECT DISTINCT meta_value
FROM ". $wpdb->postmeta ."
WHERE meta_key = 'resort_cf'
ORDER BY meta_value
");
foreach ($posts as $key => $post) {
$values[$post] = get_post( $post )->post_title;
}
?>
<select name="ADMIN_FILTER_FIELD_VALUE">
<option value=""><?php _e('All Rooms ', 'wose45436'); ?></option>
<?php
$current_v = isset($_GET['ADMIN_FILTER_FIELD_VALUE'])? $_GET['ADMIN_FILTER_FIELD_VALUE']:'';
foreach ($values as $post_key => $post_val) {
printf
(
'<option value="%s"%s>%s</option>',
$post_key,
$post_key == $current_v? ' selected="selected"':'',
$post_val
);
}
?>
</select>
<?php
}
}
add_filter( 'parse_query', 'room_posts_filter' );
/**
* if submitted filter by post meta
*
* make sure to change META_KEY to the actual meta key
* and POST_TYPE to the name of your custom post type
* @author Ohad Raz
* @param (wp_query object) $query
*
* @return Void
*/
function room_posts_filter( $query ){
global $pagenow;
$type = 'post';
if (isset($_GET['post_type'])) {
$type = $_GET['post_type'];
}
if ( 'room' == $type && is_admin() && $pagenow=='edit.php' && isset($_GET['ADMIN_FILTER_FIELD_VALUE']) && $_GET['ADMIN_FILTER_FIELD_VALUE'] != '') {
$query->query_vars['meta_key'] = 'resort_cf';
$query->query_vars['meta_value'] = $_GET['ADMIN_FILTER_FIELD_VALUE'];
}
}
<?php
/*
Plugin Name: Admin Filter BY Custom Fields
Plugin URI: http://en.bainternet.info
Description: answer to http://wordpress.stackexchange.com/q/45436/2487
Version: 1.0
Author: Bainternet
Author URI: http://en.bainternet.info
*/
add_action( 'restrict_manage_posts', 'wpse45436_admin_posts_filter_restrict_manage_posts' );
/**
* First create the dropdown
* make sure to change POST_TYPE to the name of your custom post type
*
* @author Ohad Raz
*
* @return void
*/
function wpse45436_admin_posts_filter_restrict_manage_posts(){
$type = 'post';
if (isset($_GET['post_type'])) {
$type = $_GET['post_type'];
}
//only add filter to post type you want
if ('POST_TYPE' == $type){
//change this to the list of values you want to show
//in 'label' => 'value' format
$values = array(
'label' => 'value',
'label1' => 'value1',
'label2' => 'value2',
);
?>
<select name="ADMIN_FILTER_FIELD_VALUE">
<option value=""><?php _e('Filter By ', 'wose45436'); ?></option>
<?php
$current_v = isset($_GET['ADMIN_FILTER_FIELD_VALUE'])? $_GET['ADMIN_FILTER_FIELD_VALUE']:'';
foreach ($values as $label => $value) {
printf
(
'<option value="%s"%s>%s</option>',
$value,
$value == $current_v? ' selected="selected"':'',
$label
);
}
?>
</select>
<?php
}
}
add_filter( 'parse_query', 'wpse45436_posts_filter' );
/**
* if submitted filter by post meta
*
* make sure to change META_KEY to the actual meta key
* and POST_TYPE to the name of your custom post type
* @author Ohad Raz
* @param (wp_query object) $query
*
* @return Void
*/
function wpse45436_posts_filter( $query ){
global $pagenow;
$type = 'post';
if (isset($_GET['post_type'])) {
$type = $_GET['post_type'];
}
if ( 'POST_TYPE' == $type && is_admin() && $pagenow=='edit.php' && isset($_GET['ADMIN_FILTER_FIELD_VALUE']) && $_GET['ADMIN_FILTER_FIELD_VALUE'] != '') {
$query->query_vars['meta_key'] = 'META_KEY';
$query->query_vars['meta_value'] = $_GET['ADMIN_FILTER_FIELD_VALUE'];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment