Admin menu column and filter
<?php
/*********************
ADMIN COLUMNS
*********************/
/*
*
* Note that the name of the filter changes based
* on the custom post type you are using
* Our CPT this example is location_menu
* Replace location_menu in each filter you see
* with the name of your custom post type if you
* use this code #kthxbai :-D
*
* https://catapultthemes.com/add-acf-fields-to-admin-columns/
* http://justintadlock.com/archives/2011/06/27/custom-columns-for-custom-post-types
* https://wpdreamer.com/2014/04/how-to-make-your-wordpress-admin-columns-sortable/
* http://wordpress.stackexchange.com/questions/210424/filters-post-in-admin-with-dropdown-select-custom-post-type/210524
*
*/
/*
*
* Add the column to the 'location_menu' admin area for the 'Location'
* This is adding two custom fields (location, core_menu_item) to a
* custom post type (locations_menu)
*
*/
function add_acf_columns ( $columns ) {
return array_merge ( $columns, array (
'location' => __ ( 'Location' ),
'core_menu_item' => __ ( 'Core menu item' )
) );
}
add_filter ( 'manage_location_menu_posts_columns', 'add_acf_columns' );
/*
*
* Populate each column with a value, otherwise it's empty
*
*/
function location_menu_custom_column ( $column, $post_id ) {
switch ( $column ) {
case 'location':
echo get_the_title( get_post_meta ( $post_id, 'location', true ));
break;
case 'core_menu_item':
echo get_the_title(get_post_meta ( $post_id, 'core_menu_item', true ));
break;
}
}
add_action ( 'manage_location_menu_posts_custom_column', 'location_menu_custom_column', 10, 2 );
/*
*
* You thought we were done? Nope. Register the column(s) as sortable
* In this case we only really needed the location column sortable
*
*/
function location_menu_register_sortable( $columns )
{
$columns['location'] = 'location';
return $columns;
}
add_filter("manage_edit-location_menu_sortable_columns", "location_menu_register_sortable" );
/* This is where you actuall do the sorting. The code above only registers them and doesn't do any sorting. */
add_action( 'pre_get_posts', 'manage_wp_posts_be_qe_pre_get_posts', 1 );
function manage_wp_posts_be_qe_pre_get_posts( $query ) {
/**
* We only want our code to run in the main WP query
* AND if an orderby query variable is designated.
*/
if ( $query->is_main_query() && ( $orderby = $query->get( 'orderby' ) ) ) {
switch( $orderby ) {
// If we're ordering by 'exclusive'
case 'location':
// set our query's meta_key, which is used for custom fields
$query->set( 'meta_key', 'location' );
/**
* Tell the query to order by our custom field/meta_key's
* value, in this film rating's case: PG, PG-13, R, etc.
*
* If your meta value are numbers, change 'meta_value'
* to 'meta_value_num'.
*/
$query->set( 'orderby', 'meta_value' );
break;
}
}
}
/*********************
ADMIN FILTERS -- We want to filter by location, too
*********************/
add_action( 'restrict_manage_posts', 'wpse45436_admin_posts_filter_restrict_manage_posts' );
/**
* First create the dropdown
* make sure to change the post_type in the IF ''== $type to the name of your custom post type
*
* @originalauthor Ohad Raz @modifedby Caitlin DiMare-Oliver
*
* @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 ('location_menu' == $type){
//change this to the list of values you want to show
//in 'label' => 'value' format
$args = array(
'numberposts' => -1,
'post_type' => 'rosatis_locations'
);
$locations = get_posts( $args );
foreach ($locations as $location) {
$values[$location->post_title] = $location->ID;
}
?>
<select name="ADMIN_FILTER_FIELD_VALUE">
<option value=""><?php _e('Filter By Location', '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', 'location_menu_posts_filter' );
/**
* if submitted filter by post meta
*
* make sure to change 'location' to the actual meta key
* and POST_TYPE to the name of your custom post type
* @originalauthor Ohad Raz @modifedby Caitlin DiMare-Oliver
* @param (wp_query object) $query
*
* @return Void
*/
function location_menu_posts_filter( $query ){
global $pagenow;
$type = 'post'; // Don't change this. It's tempting. Don't.
if (isset($_GET['post_type'])) { // Don't change this. It's tempting. Don't.
$type = $_GET['post_type']; // Don't change this. It's tempting. Don't.
}
// Here's where you change things. Swap out 'location_menu' and 'location' for your custom
// Post type (where you want it to show up in the admin) and the custom field (the one we're filtering by)
if ( 'location_menu' == $type && is_admin() && $pagenow=='edit.php' && isset($_GET['ADMIN_FILTER_FIELD_VALUE']) && $_GET['ADMIN_FILTER_FIELD_VALUE'] != '') {
$query->query_vars['meta_key'] = 'location';
$query->query_vars['meta_value'] = $_GET['ADMIN_FILTER_FIELD_VALUE'];
}
}
?>