Support

Account

Home Forums ACF PRO Post object selects are empty

Solving

Post object selects are empty

  • Greetings,

    I’m working on a customer’s website using ACF Pro, and I encountered an issue I can’t resolve.

    The site uses, for post articles in a specific category (let’s say A), an ACF field to link them with another post from another category (B). The post from A has to be linked to at least one post from B.

    For this, the previous site developer site (not my agency) used a post object field. It is required, filtered by post type “post” and by taxonomy “category:B”. An empty value cannot be selected, but more than one can.

    My problem is, in the case of a new post, that the select field is empty, and for an older post, the select field only contains the related posts already selected beforehand.

    Since I’m no slouch in PHP, I dove in the function, but I’m stuck on some code I can’t understand :

    In “class-acf-field-post_object.php”, the select options are populated in the render_field() function with the results of
    $posts = $this->get_posts( $field['value'], $field );
    As I understand it, its parameters are the actual value of the field, and the field object itself.
    Next, get_posts() (same file) puts the value of the field in the ‘post__in’ parameter.

    $posts = acf_get_posts(array(
     'post__in' => $value,
     'post_type' => $field['post_type']
    ));

    Next stop, function acf_get_posts() in “api-helpers.php”, where a few more parameters are added, and then
    $posts = get_posts($args);
    Which results with nothing, or next to nothing.

    Here are my two problems :

    1. Why does the posts query seem to be only on the field value, which is only the posts already attached to this field, and not the whole posts pool
    2. Why can’t I see anywhere how the selected taxonomies are put in the query parameters ?

    I tried a few things, like commenting 'post__in' => $value, in the function above, and then the query returned me each and every post on the site, not just those in category B (therefore my problem on the taxonomies).

    I updated the plugin before rampaging in the code, but that didn’t solve the problem.

    So, did I miss something ? Is my ACF poorly set up ?

  • For the post object field, render_field() only include what has been already selected. This is a select2 field and population of the rest of the field depends on an AJAX request to get the values.

    I would start by looking for a JavaScript error, or a PHP error that could be interfering with ACF returning values during the request.

    To see PHP errors that happen during the request you need to turn on error logging https://codex.wordpress.org/WP_DEBUG. Basically, anything that creates unexpected output during the AJAX request will cause the request to fail and this could be why the choices are not being populated.

    On the other hand it could be a JS error before the request that prevents it from happening.

  • Okay, I understand a tad better how all of this is running, but still, I haven’t solved my issue.

    According to what you said, I dug my way from the called AJAX (with action: acf/post/get_field_groups) but again, I’m stuck in the function acf_get_value() in file “api-value.php”, where, I understand, the value are fetched for filling the field.

    Several filters are applied but none return anything :

    $value = acf_get_metadata( $post_id, $field['name'] );
    ...
    $value = apply_filters( "acf/load_value", $value, $post_id, $field );
    $value = apply_filters( "acf/load_value/type={$field['type']}", $value, $post_id, $field );
    $value = apply_filters( "acf/load_value/name={$field['_name']}", $value, $post_id, $field );
    $value = apply_filters( "acf/load_value/key={$field['key']}", $value, $post_id, $field );

    (This is for a new post, for an older post, acf_get_metadata returns the values for the field already associated with the post)

    I printed the output of each call, but they all come back empty.

    Furthermore, the load_value() function from “class-acf-field-post_object.php” looks like this :

    function load_value( $value, $post_id, $field ) {
    	// ACF4 null
    	if( $value === 'null' ) return false;
    		// return
    		return $value;
    		
    }

    This function doesn’t look really useful to me… I checked on other class files, “class-acf-field-select.php” has the same function, but other files (ex “class-acf-field-taxonomy.php”) have real code for this function, so, even if I updated the plugin yesterday, I wondered if some files have been altered and not updated, or if these function are the real ones.

    I also checked about select2, since you mentionned it, I found a get_ajax_query() function in “class-acf-field-post_object.php” (“This function will return an array of data formatted for use in a select2 AJAX response”) which seems to do what I want, but I put some tracker in it, and it doesn’t seem to be called, so maybe the problem is this function should be the one called, but because of some bug, it isn’t and another one is instead ?

  • The function works the way it’s supposed to, like I said, as long as there isn’t any errors. I’m not having any issues with ACF working correctly. I think you’re going down the wrong path by trying to figure out what ACF is doing.

    To answer your questions, the load_value() method in each field class does what it needs to do base on the field type. The post object fields for examples returns a post ID, or an array or post IDs if something is saved or it returns flash if nothing is saved. On the other hand the taxonomy field has a lot more work to do. All of the functions in ACF are real function, they all do something.

    I’m not sure what you mean by adding tracking to the ajax function. This will only run when the ajax request is made. If the function is never run then I would say that there is a JavaScript error that prevents the request from being made in the first place. This goes back to my first reply.

    Have you checked for JavaScript errors? Or php errors during the AJAX request?

  • I am having the exact same problem with my multisite. Everything works well in the back-end, but when I try to use front-end form to create a new post, post object field doesn’t populate any choices. Or if I choose something and save it from back-end, it only shows that selected option on the front-end.

    Other fields that use select2 (user field etc.) also doesn’t populate any choices on the front-end form.

    I am using acf_form function

    I am using wordpress 5.4 and ACF 5.8.9

    *I don’t have any JS errors on console
    *I don’t have any PHP errors on WP_DEBUG
    *I tried to disable all other plugins but that didn’t work either
    *I tried every possible placement of wp_footer and acf_form_head to give best priority to loading of acf components
    *my custom theme doesn’t contain any other AJAX requests
    *I read all forum threads and google search results about similiar errors.

    Still couldn’t find an answer.

  • Hi, ulascansadi. I ran into this problem and successfully got it solved today. Please check whether you used any function where ‘date’ is mentioned. In my case, one of my custom fields is called ‘date’ and then I wrote a function to make the column of this custom field sortable, which caused the problem.

    function cpt_date_orderby( $query ) {
     $orderby = $query->get( 'orderby' );
     if( 'date' == $orderby ) {
        $query->set('meta_key', 'date');
        $query->set('orderby', 'meta_value_num');
     }
    }
    add_action( 'pre_get_posts', 'cpt_date_orderby' );

    The problem lies in line 3, where ‘date’ is value for the parameter. I replaced ‘date’ with a different string, like ‘date1’, and that’s why I solved the problem.

    It seems ‘date’ is a reserved word in WordPress, and should be used carefully.

  • Hi, ulascansadi. I ran into this problem and successfully got it solved today. Please check whether you used any function where ‘date’ is mentioned. In my case, one of my custom fields is called ‘date’ and then I wrote a function to make the column of this custom field sortable, which caused the problem.

    function cpt_date_orderby( $query ) {
     $orderby = $query->get( 'orderby' );
     if( 'date' == $orderby ) {
        $query->set('meta_key', 'date');
        $query->set('orderby', 'meta_value_num');
     }
    }
    add_action( 'pre_get_posts', 'cpt_date_orderby' );

    The problem lies in line 3, where ‘date’ is value for the parameter. I replaced ‘date’ with a different string, like ‘date1’, and that’s why I solved the problem.

    It seems ‘date’ is a reserved word in WordPress, and should be used carefully.

  • @hkmakchui the issue with your filter is that it is running every time a query is done, and this includes queries that ACF runs to get the results for fields.

    You should have conditionals at the top of every pre_get_posts filter so that it only runs when you need it to.

    For example: If you don’t want it to run in the admin

    
    if (is_admin()) {
      return;
    }
    

    or if you only want it to run on the main query

    
    if (!$query->is_main_qieru()) {
      return;
    }
    
  • Thanks, John. I amended the function accordingly and this was what I got.

    
    function cpt_date_orderby( $query ) {
     if (!$query->is_main_qieru()) {
      return;
     }
     $orderby = $query->get( 'orderby' );
     if( 'date' == $orderby ) {
        $query->set('meta_key', 'date');
        $query->set('orderby', 'meta_value_num');
     }
    }
    add_action( 'pre_get_posts', 'cpt_date_orderby' );
    

    Did I get it wrong?

  • That should prevent your filter from running when ACF is populating the field

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

The topic ‘Post object selects are empty’ is closed to new replies.