Custom Admin Column: Uncaught Error: Class "MBAC\Post" not found
Support › MB Admin Columns › Custom Admin Column: Uncaught Error: Class "MBAC\Post" not found
- This topic has 4 replies, 2 voices, and was last updated 5 months ago by
Peter.
-
AuthorPosts
-
November 15, 2024 at 11:08 PM #46940
FizzPop Media
ParticipantI am using the documentation here to create custom code to include the slug of the post in the admin columns.
https://docs.metabox.io/extensions/mb-admin-columns/#custom-admin-columnsThe first error I get is:
PHP Fatal error: Uncaught Error: Class "MBAC\Post" not found
What am I missing?
I am running Metabox 5.10.3 and AIO 1.30.5
<div> class Prefix_Custom_Admin_Columns extends \MBAC\Post { public function columns( $columns ) { $columns = parent::columns( $columns ); $position = 'after'; $target = 'title'; $this->add( $columns, 'post_name', 'Slug', $position, $target ); // Add more if you want return $columns; } public function show( $column, $post_id ) { switch ( $column ) { case 'post_name': echo $post->post_name;; break; // More columns } } } </div>
<div> Here is the function: add_action( 'admin_init', 'prefix_add_custom_columns', 20 ); function prefix_add_custom_columns() { require_once 'custom.php'; $post_type = 'post'; new Prefix_Custom_Admin_Columns( $post_type, [] ); } </div>
November 18, 2024 at 10:34 PM #46956Peter
ModeratorHello,
Do you add the file custom.php and the code to a custom plugin? Maybe the plugin is executed before Meta Box plugins so the Meta Box features are not available and cause the issue. Please try to add the file and custom code to a theme or child theme folder and check the issue again.
November 19, 2024 at 2:29 AM #46960FizzPop Media
ParticipantI have resolved the previous issue.
Now I am trying to add a custom field for any custom post type registered with metabox.
If I hardcode the values of postTypes
vendor
andhome
in the array it works fine.
However, I can't figure out how to query an array of all postTypes that exist. Please advise.`
add_action('admin_init', 'prefix_add_custom_columns', 20);function prefix_add_custom_columns() {
require_once 'custom-admin-columns.php';
$post_types = ['vendor';'home'];
if (!empty($post_types)) {
foreach ($post_types as $post_type) {
// Apply custom admin columns to each post type
new Prefix_Custom_Admin_Columns($post_type, []);
}
}
}
`
November 19, 2024 at 2:29 AM #46961FizzPop Media
Participantadd_action('admin_init', 'prefix_add_custom_columns', 20); function prefix_add_custom_columns() { require_once 'custom-admin-columns.php'; $post_types = ['vendor';'home']; if (!empty($post_types)) { foreach ($post_types as $post_type) { // Apply custom admin columns to each post type new Prefix_Custom_Admin_Columns($post_type, []); } } }
November 19, 2024 at 11:14 PM #46971Peter
ModeratorHello,
You can use the WordPress function
get_post_types()
to get available post types on your site.
https://developer.wordpress.org/reference/functions/get_post_types/ -
AuthorPosts
- You must be logged in to reply to this topic.