carbon-fields icon indicating copy to clipboard operation
carbon-fields copied to clipboard

How get current post ID in field definition?

Open zevilz opened this issue 4 years ago • 8 comments

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

zevilz avatar Feb 22 '21 13:02 zevilz

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 avatar Feb 22 '21 21:02 dimitriBouteille

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

zevilz avatar Feb 23 '21 10:02 zevilz

And this ?

$currentID = $_GET['post'] ?? $_POST['id'] ?? null;

dimitriBouteille avatar Feb 23 '21 11:02 dimitriBouteille

I already tried this but it doesn't work.

zevilz avatar Feb 23 '21 20:02 zevilz

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 avatar Feb 25 '21 10:02 jorostoyanov

@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;
}

image

zevilz avatar Feb 26 '21 07:02 zevilz

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.

zevilz avatar Feb 26 '21 08:02 zevilz

This should work: $post_id = $_GET['post'] ?? $_POST['id'] ?? $_POST['post_ID'];

RaduDragomir avatar Mar 11 '22 16:03 RaduDragomir