rwmb_meta_boxes filter
- This topic has 7 replies, 2 voices, and was last updated 8 years, 2 months ago by
Anh Tran.
-
AuthorPosts
-
February 6, 2017 at 4:14 PM #4974
woorise
ParticipantHello,
I use something like the following code inside the rwmb_meta_boxes filter for a custom post type.
Is there any recommended way to make it run only on current custom post type. For example it also runs on post's edit screen and I get a PHP notice "Undefined variable: content"
if ( get_post_status( $post_id ) != 'publish' ) { $content = 'Not Published'; } else { $content = 'Published'; }
February 7, 2017 at 8:23 AM #4984Anh Tran
KeymasterHi,
How do you get the
$post_id
and how do you use the code above inside your code to register meta boxes?February 7, 2017 at 3:00 PM #4988woorise
ParticipantThe code is something like this:
add_filter( 'rwmb_meta_boxes', 'test' ); function test( $meta_boxes ) { // Get current post ID $post_id = false; if ( isset( $_GET['post'] ) ) { $post_id = intval( $_GET['post'] ); } elseif ( isset( $_POST['post_ID'] ) ) { $post_id = intval( $_POST['post_ID'] ); } if ( get_post_status( $post_id ) != 'publish' ) { $content = 'Not Published'; } else { $content = 'Published'; } $meta_boxes[] = array( 'title' => esc_html__( 'Test', 'test' ), 'post_types' => 'testtype', 'context' => 'side', 'priority' => 'low', 'fields' => array( array( 'id' => "_test", 'type' => 'custom_html', 'std' => $content, ), ) ); return $meta_boxes; }
February 10, 2017 at 10:50 AM #5038Anh Tran
KeymasterI've tested your code and it works just fine: http://prntscr.com/e6t5gi
Did you add some conditions before assigning value to
$content
? The error says "Undefined variable: content", so it must be not initialized somewhere.February 10, 2017 at 1:44 PM #5046woorise
ParticipantI know that is working fine. My issue is that I am getting an "Undefined variable: content" notice by Query monitor on WordPress posts.
My question is if there is any recommended way to make it run only on current custom post type? For example it also runs on post’s edit screen that's why I am getting the PHP notice. I am working on a multisite installation and the code is placed in a Plugin.
Thank you.
February 10, 2017 at 5:13 PM #5050Anh Tran
KeymasterCan you clarify the "current custom post type"? What is the "current" here?
From your code above, the variable is always set by the if-else statement, so maybe the error is for somewhere else.
If you want to run the code only in the edit screen, you can add condition for the meta boxes, like is_admin(). But in that case the
rwmb_meta
function won't work properly in the frontend. See this for more details.February 11, 2017 at 8:03 PM #5062woorise
ParticipantThe current is the one that is defined in the
rwmb_meta_boxes
filter:'post_types' => 'testtype'
I need to load the
$content
variable only on thetesttype
and not on the default post type.February 13, 2017 at 1:34 PM #5072Anh Tran
KeymasterA simple solution is creating 2 meta boxes: 1 for your
testtype
where you use$content
, 1 for the other post type. -
AuthorPosts
- You must be logged in to reply to this topic.