Custom Fields
Re-Envisioned
Support General rwmb_meta_boxes filter

  • Creator
    Topic
  • #4974
    woorise
    Participant

    Hello,

    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';
    }
Viewing 7 replies - 1 through 7 (of 7 total)
  • Author
    Replies
  • #4984
    Anh Tran
    Keymaster

    Hi,

    How do you get the $post_id and how do you use the code above inside your code to register meta boxes?

    #4988
    woorise
    Participant

    The 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;
    }
    #5038
    Anh Tran
    Keymaster

    I’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.

    #5046
    woorise
    Participant

    I 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.

    #5050
    Anh Tran
    Keymaster

    Can 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.

    #5062
    woorise
    Participant

    The 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 the testtype and not on the default post type.

    #5072
    Anh Tran
    Keymaster

    A simple solution is creating 2 meta boxes: 1 for your testtype where you use $content, 1 for the other post type.

Viewing 7 replies - 1 through 7 (of 7 total)
  • You must be logged in to reply to this topic.