I spotted an issue with my original javascript, I was returning to early at the end of my script, so my ajax request would of have not completed by the time this end return was hit.
So I’ve fixed this issue using this code below…
// select2 ajax results filter
acf.add_filter('select2_ajax_results', function( json, params, instance ){
// if field id is packaging options select (second select field)
if(instance.data.field.data.key === 'field_60cd1751993d1') {
// get the current post id value of the previous select field
let post_id = instance.data.field.$el.prev('.acf-field-select').find('SELECT').val();
// if we got post id else false
if(post_id) {
post_id = parseInt(post_id);
} else {
post_id = false;
}
// jquery ajax call
$.ajax({
cache: false,
timeout: 30000,
url: ajaxurl,
type: 'GET',
data: {
action: 'get_packaging_options',
post_id: post_id
},
dataType: 'json',
success: function (response) {
// add our array to json results array
json.results = response.data;
}
}).then(function () {
// log our updated json
console.log(json);
// return updated json
return json;
});
} else {
// log our default json
console.log(json);
// return default json
return json;
}
});
So now when I click on my second select2 field, this is the updated json console log…
{
limit: 0,
more: false,
results: Array(6)
0: {id: "60cd5553b7a86", text: "Green"}
1: {id: "60cd5565b7a87", text: "Orange"}
2: {id: "60cd556db7a88", text: "Yellow"}
3: {id: "60cd5575b7a89", text: "Blue"}
4: {id: "60cd5586b7a8a", text: "Cyan"}
5: {id: "60cd558cb7a8b", text: "Red"}
length: 6
}
Log looks good, but instead of the the select2 dropdown saying No Results Found, it now says Searching…
But still my results are not loading?
I am using add_filter('acf/load_field/name=supplier', [ $this, 'load_supplier_choices' ]);
to render my select field choices.
See function below.
/**
* @param $field
* @return array
*/
public function load_provision_choices( $field ) {
// global post
global $post;
var_dump($post);
// return
return $field;
}
However when I use Use AJAX to lazy load choices?
, the AJAX response for my var_dump($post)
returns null.
How can I get the current $post
object` when lazy loading choices?
Thanks
I just had exactly the same issue. Thank god I saw this or I would of spent ages trying to figure it out.
I had to rename my post type order
to purchase_order
and that fixed problem.
So weird.
I wasn’t using Custom Post Type UI plugin, I just had my post type hard coded with this…
<?php
class Types {
/**
* Types constructor.
* @param null|int $id The Player ID to load
*/
public function __construct ($id = null) {
if(is_null($id)) {
// on initial theme load, run various actions
add_action('init', array ($this, 'action_init'));
}
}
/**
* Method to run on WordPress initialisation
*
* @uses init action
* @see https://codex.wordpress.org/Plugin_API/Action_Reference/init
*/
public function action_init () {
// register our post type
$this->register_post_types();
}
/**
* Returns an array of order labels available
* to the labels post type
*
* @param null|string $key The specific label to load
* @return array|string Array of labels or single
* if key is defined
*/
public static function order_labels ($key = null) {
// create our array of labels
$aLabels = array (
'name' => 'Orders',
'singular_name' => 'Order',
'add_new' => 'Add order',
'add_new_item' => 'Add new order',
'edit_item' => 'Edit order',
'new_item' => 'Create order',
'view_item' => 'View order',
'search_items' => 'Search orders',
'not_found' => 'No orders found',
'not_found_in_trash' => 'No orders found in trash',
'parent_item_colon' => 'Parent order:',
'all_items' => 'All orders',
'archives' => 'Order archives',
'insert_into_item' => 'Insert within order',
'uploaded_to_this_item' => 'Uploaded to order',
);
// check if we are returning a single label
if(!is_null($key) && array_key_exists($key, $aLabels)) {
return $aLabels[$key];
}
// by default return all labels
return $aLabels;
}
/**
* Registers post types
* @return void
*/
protected function register_post_types () {
// register our field post type product
register_post_type('order', array (
// define our post type labels
'label' => self::order_labels('singular_name'),
'labels' => self::order_labels(),
// make post type public queryable
'public' => true,
// setup UI preferences
'show_ui' => true,
'show_in_nav_menus' => true,
'show_in_menu' => true,
'show_in_admin_bar' => false,
// set our menu position and icon
'menu_position' => 50,
'menu_icon' => 'dashicons-list-view',
// determine our capabilities & support
'capability_type' => 'post',
'supports' => array (
'title', 'author', 'editor'
),
// archiving and linking
'has_archive' => false,
'rewrite' => array (
'slug' => 'order',
'with_front' => false,
'feeds' => false,
'pages' => false,
'ep_mask' => EP_PERMALINK,
),
));
}
} new Types();
Thanks, I’ve just submitted a ticket.
Thanks for the quick response. What I’ve done as a temporary fix is bind onto the acf/render_field
action and targeted a message input type inside an ACF meta box… seems to do the job quite nicely 🙂
Great plugin all in all, we wouldn’t be anywhere without it! It’s the only plugin we always use.
Just run into this issue… Would be great to see a fix rather than a workaround!
Perfect, does the job and also makes the attachment_fields_to_save hook redundant – thanks!
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.