Skip to content

Instantly share code, notes, and snippets.

@billerickson
Created September 14, 2012 14:43
Show Gist options
  • Save billerickson/070476e584b04a20c770 to your computer and use it in GitHub Desktop.
Save billerickson/070476e584b04a20c770 to your computer and use it in GitHub Desktop.
<?php
add_filter( 'cmb_meta_boxes' , 'be_metaboxes' );
/**
* Create Metaboxes
*
* @link http://www.billerickson.net/wordpress-metaboxes/
*
*/
function be_metaboxes( $meta_boxes ) {
$meta_boxes[] = array(
'id' => 'be-cat-2',
'title' => 'Category 2 Information',
'pages' => array('post'),
'show_on' => array(
'key' => 'taxonomy',
'value' => array(
'category' => 'cat-2',
'rotator-location' => array( 'location-1', 'location-3' )
)
),
'context' => 'normal',
'priority' => 'high',
'show_names' => true,
'fields' => array(
array(
'name' => 'Subtitle',
'desc' => '',
'id' => 'be_post_subtitle',
'type' => 'text'
),
),
);
return $meta_boxes;
}
@lonchbox
Copy link

lonchbox commented Mar 1, 2013

To make it work I add the filter in init.php, and then this gist works but I´m not sure it´s correct.

@hansengine
Copy link

I'm using a custom post type and custom taxonomy, but not working for me.

Any idea?.. Thank you.

Copy link

ghost commented Sep 23, 2014

I had the same problem as @hansengine. Here's the solution:

Add the code from: https://github.com/WebDevStudios/Custom-Metaboxes-and-Fields-for-WordPress/wiki/Adding-your-own-show_on-filters#example-taxonomy-show_on-filter to your CMB2_Show_fiters.php. I put it after the 'get_show_on_key' filter, and changed the name to "get_show_on_taxonomy", and removed the line where it adds the filter, as it's redundant. I also made the function public static, not sure if that did anything.

Included
'object_types' => array('my-custom-post-type')

Then, for 'show_on" in the $meta_boxes declaration, I put the following:
'key' => 'taxonomy', 'value' => array( 'my-custom-taxonomy' => 'my-custom-term')

@protasisimone
Copy link

I can't understand how it works.
Where should put this code? In init.php?
And this, where is put?

function be_taxonomy_show_on_filter( $display, $meta_box ) {

if ( 'taxonomy' !== $meta_box['show_on']['key'] )
    return $display;

if( isset( $_GET['post'] ) ) $post_id = $_GET['post'];
elseif( isset( $_POST['post_ID'] ) ) $post_id = $_POST['post_ID'];
if( !isset( $post_id ) )
    return $display;

foreach( $meta_box['show_on']['value'] as $taxonomy => $slugs ) {
    if( !is_array( $slugs ) )
        $slugs = array( $slugs );

    $display = false;           
    $terms = wp_get_object_terms( $post_id, $taxonomy );
    foreach( $terms as $term )
        if( in_array( $term->slug, $slugs ) )
            $display = true;
}

return $display;</code>
} add_filter( 'cmb_show_on', 'be_taxonomy_show_on_filter', 10, 2 );

My taxonomy is standard wordpress category and the term is "travel", how do I make visible the Metabox only on that term?

@wasimgit
Copy link

I am using standard categories, not work for me, please suggest.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment