Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

CMB metaboxes show_on filter WordPress


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=""]]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

Answers (1)


Dbranes answers:

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 <em>ed_metabox_include_front_page()</em> 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 <em>frontpage</em> page and the <em>place</em> posts.

designbuildtest comments:

Superb, many thanks Dbranes. The suggested function does 80% of what I need.
The required metabox is missing when I first create a new Place entry, but appears after a Place entry has been Published.
Any suggestions?
Thanks again.

Dbranes comments:

Great, ok there's no GET parameter containing the post ID, for new posts, so let my try to update it.

Dbranes comments:

I updated the answer.

Dbranes comments:

ps: here's a simplified version:

function wpq_metabox_include_front_page_and_places_v2( $display, $meta_box ) {

if ( ! isset( $meta_box['show_on']['key'] ) || 'front-page-and-places' !== $meta_box['show_on']['key'] ){
return false;

$_post = filter_input( INPUT_GET, 'post', FILTER_SANITIZE_NUMBER_INT );
$_post_type = filter_input( INPUT_GET, 'post_type', FILTER_SANITIZE_STRING );

$pid = ( ! empty( $_post ) ) ? $_post : '' ;
$pt = ( ! empty( $_post_type ) ) ? $_post_type : get_post_type( $pid );

if ( $pid != get_option('page_on_front') && 'place' !== $pt ) {
$display = false;

return $display;

add_filter( 'cmb_show_on', 'wpq_metabox_include_front_page_and_places_v2', 10, 2 );

designbuildtest comments:

Fabulous, thanks Dbranes. 95% there now!
Only remaining issue is when I update any meta value on my front/homepage, the changed value is not saved.
All meta changes are successful when creating or editing a Place however.

designbuildtest comments:

Just wondering, would:
work as a conditional to check if on the create New Place screen?

Dbranes comments:

Maybe we are getting into one of the [[LINK href="'s_paradoxes"]]Zeno's paradoxes[[/LINK]], where I will never catch the "WordPress Problem Turtle" ;-)

I think the <em>$screen</em> and <em>$post</em> objects are not available when the <em>cmb_show_on</em> filter is activated.

<blockquote>Only remaining issue is when I update any meta value on my front/homepage, the changed value is not saved.

It looks like I would need some more info regarding this setup. Does it work without the filter applied?

designbuildtest comments:

Apologies for my slow response Dbranes - [[LINK href=""]]New Zealand's Internet broke this weekend[[/LINK]] ...

To answer your question, yes, metafields update without the filter applied and also when Ed Townend's original filter is used.

Maybe some inline CSS could be applied to the admin area to only show my location metabox on the frontpage and place entries? A bit hacky, but would be fine in this instance.

Even if this question remains unsolved, you deserve the full award amount for your efforts.

Again, apologies for falling off the Internet for the last couple of days.

Many thanks.