Support

Account

Home Forums Backend Issues (wp-admin) Post Object Field broken when getting $_GET['post']

Solving

Post Object Field broken when getting $_GET['post']

  • I am hooking onto ‘after_setup_theme’ to render the ACF fields when editing a post type. What gets rendered depends on the post type…at this stage there is no global $post and get_post_type() is not available.

    So I thought I’d be clever and use $_GET[‘post’] to get the post ID and do a get_post_type to find the post type and render content accordingly.

    However, when I do this, the post object field stops working and displays “No results found”. This line of code breaks it all:

    if ( empty( $_GET['post'] ) || empty( $_GET['action'] ) || 'edit' !== $_GET['action'] ) {
    		return;
    	}

    If I remove that line, the post object fields gets the values.

    My guess is that by just using $_GET[‘post’], I am messing up the post object used by the post object control, but I am not sure why.

    Can anyone share shed some light? I’m obviously missing something to understand what’s going on.

    Here is a movie that shows the behaviour:

    [Movie Pending]

  • Bumping this one as I am completely baffled and it’s really annoying me. Essentially I hook up to after_setup_theme. At this stage I do not know the post ID other than by using $_GET, so this is my code:

    $acf_loader = new ACFLoader();
    		$post_id = $_GET['post'];
    		$post_type = get_post_type( $post_id );
    		call_user_func( array( $acf_loader, 'create_' . $post_type . '_fields' ) );

    When I do that, all of ACF fields functionality goes out the window (see movie above). If I do this, it works fine:

    		$acf_loader = new ACFLoader();
    		$acf_loader->create_volunteer_fields();
    		$acf_loader->create_client_fields();
    		$acf_loader->create_match_fields();
    		$acf_loader->create_area_coordinator_fields();
    		$acf_loader->create_referrer_fields();

    This class merely uses FieldsBuilder to create the fields that are needed.

    If I hook up to ‘wp’ or anything else, I am able to query the post but it’s too late for the ACF fields to take effect.

    Any pointers appreciated! 🥺

Viewing 3 posts - 1 through 3 (of 3 total)

The topic ‘Post Object Field broken when getting $_GET['post']’ is closed to new replies.