function get_choices($options = array()) { // defaults $options = acf_parse_args($options, array('post_id' => 0, 's' => '', 'lang' => false, 'field_key' => '', 'paged' => 1)); // vars $r = array(); $args = array(); // paged $args['posts_per_page'] = 20; $args['paged'] = $options['paged']; // load field $field = acf_get_field($options['field_key']); if (!$field) { return false; } // update $args if (!empty($field['post_type'])) { $args['post_type'] = acf_get_array($field['post_type']); } else { $args['post_type'] = acf_get_post_types(); } // create tax queries if (!empty($field['taxonomy'])) { // append to $args $args['tax_query'] = array(); // decode terms $taxonomies = acf_decode_taxonomy_terms($field['taxonomy']); // now create the tax queries foreach ($taxonomies as $taxonomy => $terms) { $args['tax_query'][] = array('taxonomy' => $taxonomy, 'field' => 'slug', 'terms' => $terms); } } // search if ($options['s']) { $args['s'] = $options['s']; } // filters $args = apply_filters('acf/fields/page_link/query', $args, $field, $options['post_id']); $args = apply_filters('acf/fields/page_link/query/name=' . $field['name'], $args, $field, $options['post_id']); $args = apply_filters('acf/fields/page_link/query/key=' . $field['key'], $args, $field, $options['post_id']); // add archives to $r if ($args['paged'] == 1) { $archives = array(); $archives[] = array('id' => home_url(), 'text' => home_url()); foreach ($args['post_type'] as $post_type) { $archive_link = get_post_type_archive_link($post_type); if ($archive_link) { $archives[] = array('id' => $archive_link, 'text' => $archive_link); } } // search if (!empty($args['s'])) { foreach (array_keys($archives) as $i) { if (strpos($archives[$i]['text'], $args['s']) === false) { unset($archives[$i]); } } $archives = array_values($archives); } if (!empty($archives)) { $r[] = array('text' => __('Archives', 'acf'), 'children' => $archives); } } // get posts grouped by post type $groups = acf_get_grouped_posts($args); if (!empty($groups)) { foreach (array_keys($groups) as $group_title) { // vars $posts = acf_extract_var($groups, $group_title); $titles = array(); // data $data = array('text' => $group_title, 'children' => array()); foreach (array_keys($posts) as $post_id) { // override data $posts[$post_id] = $this->get_post_title($posts[$post_id], $field, $options['post_id']); } // order by search if (!empty($args['s'])) { $posts = acf_order_by_search($posts, $args['s']); } // append to $data foreach (array_keys($posts) as $post_id) { $data['children'][] = array('id' => $post_id, 'text' => $posts[$post_id]); } // append to $r $r[] = $data; } } // return return $r; }
function render_field($field) { // vars $values = array(); $atts = array('id' => $field['id'], 'class' => "acf-relationship {$field['class']}", 'data-min' => $field['min'], 'data-max' => $field['max'], 'data-s' => '', 'data-post_type' => '', 'data-taxonomy' => '', 'data-paged' => 1); // Lang if (defined('ICL_LANGUAGE_CODE')) { $atts['data-lang'] = ICL_LANGUAGE_CODE; } // data types $field['post_type'] = acf_force_type_array($field['post_type']); $field['taxonomy'] = acf_force_type_array($field['taxonomy']); // post_types $post_types = array(); if (!empty($field['post_type'])) { $post_types = $field['post_type']; } else { $post_types = acf_get_post_types(); } $post_types = acf_get_pretty_post_types($post_types); // taxonomies $taxonomies = array(); if (!empty($field['taxonomy'])) { // get the field's terms $term_groups = acf_force_type_array($field['taxonomy']); $term_groups = acf_decode_taxonomy_terms($term_groups); // update taxonomies $taxonomies = array_keys($term_groups); } elseif (!empty($field['post_type'])) { // loop over post types and find connected taxonomies foreach ($field['post_type'] as $post_type) { $post_taxonomies = get_object_taxonomies($post_type); // bail early if no taxonomies if (empty($post_taxonomies)) { continue; } foreach ($post_taxonomies as $post_taxonomy) { if (!in_array($post_taxonomy, $taxonomies)) { $taxonomies[] = $post_taxonomy; } } } } else { $taxonomies = acf_get_taxonomies(); } // terms $term_groups = acf_get_taxonomy_terms($taxonomies); // update $term_groups with specific terms if (!empty($field['taxonomy'])) { foreach (array_keys($term_groups) as $taxonomy) { foreach (array_keys($term_groups[$taxonomy]) as $term) { if (!in_array($term, $field['taxonomy'])) { unset($term_groups[$taxonomy][$term]); } } } } // width for select filters $width = array('search' => 0, 'post_type' => 0, 'taxonomy' => 0); if (!empty($field['filters'])) { $width = array('search' => 50, 'post_type' => 25, 'taxonomy' => 25); foreach (array_keys($width) as $k) { if (!in_array($k, $field['filters'])) { $width[$k] = 0; } } // search if ($width['search'] == 0) { $width['post_type'] = $width['post_type'] == 0 ? 0 : 50; $width['taxonomy'] = $width['taxonomy'] == 0 ? 0 : 50; } // post_type if ($width['post_type'] == 0) { $width['taxonomy'] = $width['taxonomy'] == 0 ? 0 : 50; } // taxonomy if ($width['taxonomy'] == 0) { $width['post_type'] = $width['post_type'] == 0 ? 0 : 50; } // search if ($width['post_type'] == 0 && $width['taxonomy'] == 0) { $width['search'] = $width['search'] == 0 ? 0 : 100; } } ?> <div <?php acf_esc_attr_e($atts); ?> > <div class="acf-hidden"> <input type="hidden" name="<?php echo $field['name']; ?> " value="" /> </div> <?php if ($width['search'] > 0 || $width['post_type'] > 0 || $width['taxonomy'] > 0) { ?> <div class="filters"> <ul class="acf-hl"> <?php if ($width['search'] > 0) { ?> <li style="width:<?php echo $width['search']; ?> %;"> <div class="inner"> <input class="filter" data-filter="s" placeholder="<?php _e("Search...", 'acf'); ?> " type="text" /> </div> </li> <?php } ?> <?php if ($width['post_type'] > 0) { ?> <li style="width:<?php echo $width['post_type']; ?> %;"> <div class="inner"> <select class="filter" data-filter="post_type"> <option value=""><?php _e('Select post type', 'acf'); ?> </option> <?php foreach ($post_types as $k => $v) { ?> <option value="<?php echo $k; ?> "><?php echo $v; ?> </option> <?php } ?> </select> </div> </li> <?php } ?> <?php if ($width['taxonomy'] > 0) { ?> <li style="width:<?php echo $width['taxonomy']; ?> %;"> <div class="inner"> <select class="filter" data-filter="taxonomy"> <option value=""><?php _e('Select taxonomy', 'acf'); ?> </option> <?php foreach ($term_groups as $k_opt => $v_opt) { ?> <optgroup label="<?php echo $k_opt; ?> "> <?php foreach ($v_opt as $k => $v) { ?> <option value="<?php echo $k; ?> "><?php echo $v; ?> </option> <?php } ?> </optgroup> <?php } ?> </select> </div> </li> <?php } ?> </ul> </div> <?php } ?> <div class="selection acf-cf"> <div class="choices"> <ul class="acf-bl list"></ul> </div> <div class="values"> <ul class="acf-bl list"> <?php if (!empty($field['value'])) { // get posts $posts = acf_get_posts(array('post__in' => $field['value'], 'post_type' => $field['post_type'])); // set choices if (!empty($posts)) { foreach (array_keys($posts) as $i) { // vars $post = acf_extract_var($posts, $i); ?> <li> <input type="hidden" name="<?php echo $field['name']; ?> []" value="<?php echo $post->ID; ?> " /> <span data-id="<?php echo $post->ID; ?> " class="acf-rel-item"> <?php echo $this->get_post_title($post, $field); ?> <a href="#" class="acf-icon small dark" data-name="remove_item"><i class="acf-sprite-remove"></i></a> </span> </li><?php } } } ?> </ul> </div> </div> </div> <?php }
function get_ajax_query($options = array()) { // defaults $options = acf_parse_args($options, array('post_id' => 0, 's' => '', 'field_key' => '', 'paged' => 1)); // load field $field = acf_get_field($options['field_key']); if (!$field) { return false; } // vars $results = array(); $args = array(); $s = false; $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 if (!empty($field['post_type'])) { $args['post_type'] = acf_get_array($field['post_type']); } else { $args['post_type'] = acf_get_post_types(); } // 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); // bail early if no posts if (empty($groups)) { return false; } // 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] = $this->get_post_title($posts[$post_id], $field, $options['post_id'], $is_search); } // order posts by search if ($is_search && empty($args['orderby'])) { $posts = acf_order_by_search($posts, $args['s']); } // append to $data foreach (array_keys($posts) as $post_id) { $data['children'][] = $this->get_post_result($post_id, $posts[$post_id]); } // append to $results $results[] = $data; } // optgroup or single if (count($args['post_type']) == 1) { $results = $results[0]['children']; } // vars $response = array('results' => $results, 'limit' => $args['posts_per_page']); // return return $response; }
function get_choices($options = array()) { // defaults $options = acf_parse_args($options, array('post_id' => 0, 's' => '', 'lang' => false, 'field_key' => '', 'paged' => 1)); // vars $r = array(); $args = array(); // paged $args['posts_per_page'] = 20; $args['paged'] = $options['paged']; // load field $field = acf_get_field($options['field_key']); if (!$field) { return false; } // WPML if ($options['lang']) { global $sitepress; if (!empty($sitepress)) { $sitepress->switch_lang($options['lang']); } } // update $args if (!empty($field['post_type'])) { $args['post_type'] = acf_force_type_array($field['post_type']); } else { $args['post_type'] = acf_get_post_types(); } // create tax queries if (!empty($field['taxonomy'])) { // append to $args $args['tax_query'] = array(); // decode terms $taxonomies = acf_decode_taxonomy_terms($field['taxonomy']); // now create the tax queries foreach ($taxonomies as $taxonomy => $terms) { $args['tax_query'][] = array('taxonomy' => $taxonomy, 'field' => 'slug', 'terms' => $terms); } } // search if ($options['s']) { $args['s'] = $options['s']; } // 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_posts($args); if (!empty($groups)) { foreach (array_keys($groups) as $group_title) { // vars $posts = acf_extract_var($groups, $group_title); $titles = array(); // data $data = array('text' => $group_title, 'children' => array()); foreach (array_keys($posts) as $post_id) { // override data $posts[$post_id] = $this->get_post_title($posts[$post_id], $field, $options['post_id']); } // order by search if (!empty($args['s'])) { $posts = acf_order_by_search($posts, $args['s']); } // append to $data foreach (array_keys($posts) as $post_id) { $data['children'][] = array('id' => $post_id, 'text' => $posts[$post_id]); } // append to $r $r[] = $data; } // optgroup or single $post_types = acf_force_type_array($args['post_type']); // add as optgroup or results if (count($post_types) == 1) { $r = $r[0]['children']; } } // return return $r; }
function ajax_query() { // validate if (!acf_verify_ajax()) { die; } // defaults $options = acf_parse_args($_POST, array('post_id' => 0, 's' => '', 'field_key' => '', 'paged' => 1)); // vars $results = array(); $args = array(); $s = false; $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; } // load field $field = acf_get_field($options['field_key']); if (!$field) { die; } // update $args if (!empty($field['post_type'])) { $args['post_type'] = acf_get_array($field['post_type']); } else { $args['post_type'] = acf_get_post_types(); } // create tax queries if (!empty($field['taxonomy'])) { // append to $args $args['tax_query'] = array(); // decode terms $taxonomies = acf_decode_taxonomy_terms($field['taxonomy']); // now create the tax queries foreach ($taxonomies as $taxonomy => $terms) { $args['tax_query'][] = array('taxonomy' => $taxonomy, 'field' => 'slug', 'terms' => $terms); } } // filters $args = apply_filters('acf/fields/page_link/query', $args, $field, $options['post_id']); $args = apply_filters('acf/fields/page_link/query/name=' . $field['name'], $args, $field, $options['post_id']); $args = apply_filters('acf/fields/page_link/query/key=' . $field['key'], $args, $field, $options['post_id']); // add archives to $results if ($field['allow_archives'] && $args['paged'] == 1) { $archives = array(); $archives[] = array('id' => home_url(), 'text' => home_url()); foreach ($args['post_type'] as $post_type) { // vars $archive_link = get_post_type_archive_link($post_type); // bail ealry if no link if (!$archive_link) { continue; } // bail early if no search match if ($is_search && stripos($archive_link, $s) === false) { continue; } // append $archives[] = array('id' => $archive_link, 'text' => $archive_link); } // append $results[] = array('text' => __('Archives', 'acf'), 'children' => $archives); } // get posts grouped by post type $groups = acf_get_grouped_posts($args); // loop if (!empty($groups)) { 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] = $this->get_post_title($posts[$post_id], $field, $options['post_id'], $is_search); } // order posts by search if ($is_search && empty($args['orderby'])) { $posts = acf_order_by_search($posts, $args['s']); } // append to $data foreach (array_keys($posts) as $post_id) { $data['children'][] = $this->get_post_result($post_id, $posts[$post_id]); } // append to $results $results[] = $data; } } // return acf_send_ajax_results(array('results' => $results, 'limit' => $args['posts_per_page'])); }
function ajax_query() { // options $options = acf_parse_args($_GET, array('post_id' => 0, 's' => '', 'lang' => false, 'field_key' => '', 'nonce' => '')); // validate if (!wp_verify_nonce($options['nonce'], 'acf_nonce')) { die; } // vars $r = array(); $args = array(); // load field $field = acf_get_field($options['field_key']); if (!$field) { die; } // WPML if ($options['lang']) { global $sitepress; $sitepress->switch_lang($options['lang']); } // update $args if (!empty($field['post_type'])) { $args['post_type'] = acf_force_type_array($field['post_type']); } else { $args['post_type'] = acf_get_post_types(); } // create tax queries if (!empty($field['taxonomy'])) { // append to $args $args['tax_query'] = array(); // decode terms $taxonomies = acf_decode_taxonomy_terms($field['taxonomy']); // now create the tax queries foreach ($taxonomies as $taxonomy => $terms) { $args['tax_query'][] = array('taxonomy' => $taxonomy, 'field' => 'slug', 'terms' => $terms); } } // search if ($options['s']) { $args['s'] = $options['s']; } // filters $args = apply_filters('acf/fields/page_link/query', $args, $field, $options['post_id']); $args = apply_filters('acf/fields/page_link/query/name=' . $field['name'], $args, $field, $options['post_id']); $args = apply_filters('acf/fields/page_link/query/key=' . $field['key'], $args, $field, $options['post_id']); // add archives to $r $archives = array(); $archives[] = array('id' => home_url(), 'text' => home_url()); foreach ($args['post_type'] as $post_type) { $archive_link = get_post_type_archive_link($post_type); if ($archive_link) { $archives[] = array('id' => $archive_link, 'text' => $archive_link); } } $r[] = array('text' => __('Archives', 'acf'), 'children' => $archives); // get posts grouped by post type $groups = acf_get_posts($args); if (!empty($groups)) { foreach (array_keys($groups) as $group_title) { // vars $posts = acf_extract_var($groups, $group_title); $titles = array(); // data $data = array('text' => $group_title, 'children' => array()); foreach (array_keys($posts) as $post_id) { // override data $posts[$post_id] = $this->get_post_title($posts[$post_id], $field, $options['post_id']); } // order by search if (!empty($args['s'])) { $posts = acf_order_by_search($posts, $args['s']); } // append to $data foreach (array_keys($posts) as $post_id) { $data['children'][] = array('id' => $post_id, 'text' => $posts[$post_id]); } // append to $r $r[] = $data; } } // return JSON echo json_encode($r); die; }
if (!empty($field['post_type'])) { $post_types = $field['post_type']; } else { $post_types = acf_get_post_types(); } $post_types = acf_get_pretty_post_types($post_types); } // taxonomy filter $taxonomies = array(); $term_groups = array(); if ($width['taxonomy']) { // taxonomies if (!empty($field['taxonomy'])) { // get the field's terms $term_groups = acf_get_array($field['taxonomy']); $term_groups = acf_decode_taxonomy_terms($term_groups); // update taxonomies $taxonomies = array_keys($term_groups); } elseif (!empty($field['post_type'])) { // loop over post types and find connected taxonomies foreach ($field['post_type'] as $post_type) { $post_taxonomies = get_object_taxonomies($post_type); // bail early if no taxonomies if (empty($post_taxonomies)) { continue; } foreach ($post_taxonomies as $post_taxonomy) { if (!in_array($post_taxonomy, $taxonomies)) { $taxonomies[] = $post_taxonomy; } }