carbon-fields
carbon-fields copied to clipboard
How get current post ID in field definition?
I need to create a select with options depending on the ID of the current post:
Field::make( 'select', 'team_id', 'Team' )
->add_options( get_cf_teams( <post ID> ) ),
ID of the current post should be passed to get_cf_teams
function. This function returns an array of options depending on the passed ID. But the ID of the current post when loading, editing, validating and saving is in different global variables and with different keys, which makes it very difficult to get it. Also when generating edit page I have a lot of warnings in ajax requests. Is there any universal way to get the ID of the current post
- Carbon Fields: 3.2
- WordPress: 5.6
- PHP: 7.4
Hi @zevilz , can you test this ? :
Field::make( 'select', 'team_id', 'Team' )
->add_options(function() {
$currentPostId = $_GET['post'] ?? null;
$data = [];
// Do something ...
return $data;
});
@dimitriBouteille ID of the current post is in $_GET['post']
only after container has been rendered. When post is saved and data is validated, it is no longer there. Value remains empty.
And this ?
$currentID = $_GET['post'] ?? $_POST['id'] ?? null;
I already tried this but it doesn't work.
Hi @zevilz
Can you provide the code for the whole container definition and not the field only?
The $_GET['id']
is available for posts that are already saved. Otherwise, the URL looks like this - /wp-admin/post-new.php?post_type=post
and you cannot access the current post ID. Once it is saved you will be able to use the $_GET['post']
approach.
Personally, in this situation, I will use the following approach:
In case the post is not saved, I will show an HTML field that will say "Please save the Post in order to update the settings below"
Field::make( 'html', 'app_post_instructions' )
->set_html( 'Please save the Post in order to update the settings below' )
Let me know, if this makes sense :-)
@jorostoyanov
The post has already been saved and there is a post ID in the $_GET. Required parameters are substituted into the select, but the field is not saved. Apparently CF makes additional field validation when saving. During validation, there is no longer the id of the current post. An empty value is saved to the database.
Container definition:
Container::make( 'post_meta', 'Tables' )
->add_fields( [
Field::make( 'complex', 'teams_tables', 'Teams tables' )
->add_fields( [
......
Field::make( 'complex', 'teams_table', 'Teams table' )
->add_fields( [
Field::make( 'select', 'team_id', 'Team' )
->add_options( cf_get_teams_requests() ),
......
] ),
] )
] );
cf_get_teams_requests():
function cf_get_teams_requests() {
$teams = [];
$post_id = $_GET['post'] ?? $_POST['id'] ?? null;
if ( empty( $post_id ) ) {
return [];
}
...getting a list of teams...
return $teams;
}
I see only one solution to the problem: get all teams if post ID not defined. But in this case it will be possible to replace team ID with any existing team ID.
This should work:
$post_id = $_GET['post'] ?? $_POST['id'] ?? $_POST['post_ID'];