Hello, I am trying to create a custom "show_on" filter for WebDevStudio's Custom-Metaboxes-and-Fields utility (original CMB version, not the new beta version).
I have created a "Place" Custom Post Type and would like a metabox to appear on: 1) all Place entries plus, 2) my Homepage (but not other Pages).
I have tried Ed Townsend's Front page filter ([[LINK href="https://github.com/WebDevStudios/Custom-Metaboxes-and-Fields-for-WordPress/wiki/Adding-your-own-show_on-filters#example-front-page-show_on-filter"]]refer here[[/LINK]]), but the show_on filter overrides the in-built pages filter and my metabox does not appear on any place entries, only my front/homepage.
'pages' => array( 'page','place' ),
'show_on' => array( 'key' => 'front-page', 'value' => '' ),
Please could someone suggest a new show_on function.
Many thanks
Solution - 1
Hi, you can try the field configuration with:
'pages' => array( 'page', 'place' ), // Post type
'show_on' => array( 'key' => 'front-page-and-places' ),
using the following modification of the ed_metabox_include_front_page() function:
function wpq_metabox_include_front_page_and_places( $display, $meta_box ) {
// We are only looking for the 'front-page-and-places' key:
if ( ! isset( $meta_box['show_on']['key'] ) || 'front-page-and-places' !== $meta_box['show_on']['key'] ){
return $display;
}
// Input:
$_post = filter_input( INPUT_GET, 'post', FILTER_SANITIZE_NUMBER_INT );
$_post_id = filter_input( INPUT_GET, 'post_id', FILTER_SANITIZE_NUMBER_INT );
$_post_type = filter_input( INPUT_GET, 'post_type', FILTER_SANITIZE_STRING );
// Get the current ID
$pid = '';
if ( ! empty( $_post ) ) {
$pid = $_post;
} elseif ( ! empty ( $_post_id ) ) {
$pid = $_post_id;
}
// Get the current post type
$pt = '';
if( ! empty( $_post_type ) ) {
$pt = $_post_type;
} elseif ( ! empty( $pid ) ) {
$pt = get_post_type( $pid );
}
// Return false early if there is no ID
if( empty( $pid ) && 'place' !== $pt ) {
return false;
}
// Get ID of page set as front page, 0 if there isn't one
$front_page = get_option('page_on_front');
// There is a front page set and we're on it! Or we are on a 'place' post
if ( $pid == $front_page || 'place' === $pt ) {
return $display;
}
// Don't display the metabox for other conditions:
return false;
}
add_filter( 'cmb_show_on', 'wpq_metabox_include_front_page_and_places', 10, 2 );
to display it for the frontpage page and the place posts.
|