Support

Account

Home Forums General Issues Dynamically registered (via PHP) relationship fields return no results

Solved

Dynamically registered (via PHP) relationship fields return no results

  • I have a situation where I need to dynamically register some relationship fields dependent on some other post meta values saved earlier. The problem is I get “No matches found” message.

    My key if prefixed with field_ and both key and name are dynamic, so no duplicates are present at any time.

    After some debugging I found that get_ajax_query() calls $field = acf_get_field( $options['field_key'] ); which returns false, so the query is not firing.

    acf_get_field() tries to get the field by key from the database which of course does not exist – it is registered dynamically.

    Any suggestions how to get relationship fields working?

  • Aaand 10 months later I stumbled on this bug again.

    I’m registering fields dynamically:

    
    foreach( $levels as $i => $level ) {
        $name = "level_{$level->term_id}_winner";
    
        acf_add_local_field([
            'parent'        => 'group_5cb57c19cffde',
            'key'           => "field_{$name}",
            'label'         => sprintf( __( '%s level winner', 'imatch' ), $level->name ),
            'name'          => $name,
            'type'          => 'post_object',
            'post_type'     => [ 'im_registration' ],
            'taxonomy'      => [],
            'allow_null'    => true,
            'return_format' => 'id',
        ]);
    }
    
  • It sounds like the field is not added when the ACF query is run, either it is created too late or it is not created at all during the ajax request.

    Where is your code for adding the field? What file? Is the file loaded during an ajax request?

    When is your field registered? What hook are you using to trigger the function?

  • The fields are registered using acf/init action (with default priority 10) and the code is placed after CPT registration.

    Is there a way to debug select2 ajax requests?

  • In order to debug what is happening on the PHP side you need to do something like write the log file https://www.php.net/manual/en/function.error-log.php.

    I still don’t think I have a clear picture of when the field is created. The information you’ve given does not tell me if the code is run during the ajax request. Is it done in your functions.php file, or some other file that is loaded every time WP initializes?

  • The other possibility is that at the time of the ajax request that ACF/WP does not know what the post ID is of the post you’re checking the other values on. This may or may not be available. It will certainly not be in the global $post as this is not set during an ajax request. To be honest, now that I think about this, the current post ID may not event be available during the acf/init hook, depending on what admin page is being loaded. How are you getting the post ID to check the values of the other fields?

  • Yes, everything is registered in functions.php

    1. add_action( 'init', 'registerPostTypes', 0 );
    2. add_action( 'acf/init', 'addFieldsByLevel' );
    3. In addFieldsByLevel function I’m looping through taxonomy terms and adding a field for each of the term (code in the earlier post).

    And yes just before the loop I’m checking for the post ID which works on admin

    
    if ( isset( $_GET['post'] ) ) {
        $post_id = $_GET['post'];
    } else if ( isset( $_POST['post_ID'] ) ) {
        $post_id = $_POST['post_ID'];
    } else {
        return;
    }
    
  • The values you’re checking may not be set during the ajax request. To be honest, I don’t know. You can use the method or writing to the log file to see what $_POST values are available.

    
    ob_start();
    print_r($_POST);
    error_log(ob_get_clean());
    
  • Right now I have solved this by not using a post_object field type. Instead I’m using select field type and manually populating its choices via acf/load_field filter.

    Initially I thought post_object field could handle everything automatically.

  • I think that ACF uses $_POST(‘post_id’) here, but it has been a long time since I’ve tested this. post_ID and post_id would not be considered the same by PHP.

  • Actually lowercase post_id is the correct usage. Thanks!

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

The topic ‘Dynamically registered (via PHP) relationship fields return no results’ is closed to new replies.