in web/wp-content/plugins/acf-extended/includes/fields/field-advanced-link.php [621:777]
function get_ajax_query($options = array()){
// defaults
$options = acf_parse_args($options, array(
'post_id' => 0,
's' => '',
'field_key' => '',
'paged' => 1,
));
// post object
$post_object = acf_get_field_type('post_object');
// load field
$field = acf_get_field($options['field_key']);
if(!$field){
return false;
}
// vars
$results = array();
$args = array();
$is_search = false;
// paged
$args['posts_per_page'] = 20;
$args['paged'] = $options['paged'];
// search
if($options['s'] !== ''){
// strip slashes (search may be integer)
$s = wp_unslash(strval($options['s']));
// update vars
$args['s'] = $s;
$is_search = true;
}
// post_type
$args['post_type'] = acf_get_post_types();
if(!empty($field['post_type'])){
$args['post_type'] = acf_get_array($field['post_type']);
}
// taxonomy
if(!empty($field['taxonomy'])){
// vars
$terms = acf_decode_taxonomy_terms($field['taxonomy']);
// append to $args
$args['tax_query'] = array();
// now create the tax queries
foreach($terms as $k => $v){
$args['tax_query'][] = array(
'taxonomy' => $k,
'field' => 'slug',
'terms' => $v,
);
}
}
// filters
$args = apply_filters('acf/fields/post_object/query', $args, $field, $options['post_id']);
$args = apply_filters('acf/fields/post_object/query/name=' . $field['name'], $args, $field, $options['post_id']);
$args = apply_filters('acf/fields/post_object/query/key=' . $field['key'], $args, $field, $options['post_id']);
// get posts grouped by post type
$groups = acf_get_grouped_posts($args);
$archives = array();
$post_types_archives = acfe_get_post_types(array(
'include' => $field['post_type'],
'has_archive' => true,
));
foreach($post_types_archives as $post_type){
$label = acf_get_post_type_label($post_type);
$label = "{$label} Archive";
if($is_search && stripos($label, $s) === false){
continue;
}
$archives[] = array(
'id' => $post_type,
'text' => $label,
);
}
if(!empty($archives)){
// data
$results[] = array(
'text' => __('Archives', 'acfe'),
'children' => $archives,
);
}
// loop
foreach(array_keys($groups) as $group_title){
// vars
$posts = acf_extract_var($groups, $group_title);
// data
$data = array(
'text' => $group_title,
'children' => array(),
);
// convert post objects to post titles
foreach(array_keys($posts) as $post_id){
$posts[ $post_id ] = $post_object->get_post_title($posts[ $post_id ], $field, $options['post_id'], $is_search);
}
// order posts by search
if($is_search && empty($args['orderby']) && isset($args['s'])){
$posts = acf_order_by_search($posts, $args['s']);
}
// append to $data
foreach(array_keys($posts) as $post_id){
$data['children'][] = $post_object->get_post_result($post_id, $posts[ $post_id ]);
}
// append to $results
$results[] = $data;
}
// optgroup or single
$post_type = acf_get_array($args['post_type']);
if(count($post_type) === 1 && empty($post_types_archives)){
$results = $results[0]['children'];
}
// vars
$response = array(
'results' => $results,
'limit' => $args['posts_per_page'],
);
// return
return $response;
}