I'm building a metabox gallery in Wordpress for a specific page template. I have the following warning notice in the dashboard:
Warning: in_array() expects parameter 2 to be array, null given in...
The warning is for the code line: if ( in_array( $post_type, $post_types ) ) {
public function add_meta_box( $post_type ) {
$post_types = apply_filters( 'sortable_wordpress_gallery_post_types', array( 'post' ) );
$post_types = apply_filters( 'sortable_wordpress_gallery_' . $this->id . '_post_types', $post_types );
if ( in_array( $post_type, $post_types ) ) {
add_meta_box(
$this->id,
$this->title,
array( $this, 'render_meta_box_content' ),
$post_type,
$this->context,
$this->priority
);
}
}
This is the code where I specify the page template where the metabox should be displayed.
add_filter( 'sortable_wordpress_gallery_post-metabox_post_types', 'first_gallery_only_on_page' );
function first_gallery_only_on_page( $post_types ) {
global $post;
if(!empty($post)) {
$pageTemplate = get_post_meta($post->ID, '_wp_page_template', true);
if($pageTemplate == 'page-coworking.php' ) {
return array( 'page' );
}
}
}
I understand that one of my param is null, but I don't find the way to set it before using it. Thank you.
return $post_types;
at the bottom, to catch the case where $post is empty or you're not on the coworking page.