function query_posts() { // vars $r = array('next_page_exists' => 1, 'html' => ''); // options $options = array('post_type' => 'all', 'taxonomy' => 'all', 'posts_per_page' => 10, 'paged' => 1, 'orderby' => 'title', 'order' => 'ASC', 'post_status' => 'any', 'suppress_filters' => false, 's' => '', 'lang' => false, 'update_post_meta_cache' => false, 'field_key' => '', 'nonce' => '', 'ancestor' => false); $options = array_merge($options, $_POST); // validate if (!wp_verify_nonce($options['nonce'], 'acf_nonce')) { die; } // WPML if ($options['lang']) { global $sitepress; if (!empty($sitepress)) { $sitepress->switch_lang($options['lang']); } } // convert types $options['post_type'] = explode(',', $options['post_type']); $options['taxonomy'] = explode(',', $options['taxonomy']); // load all post types by default if (in_array('all', $options['post_type'])) { $options['post_type'] = apply_filters('acf/get_post_types', array()); } // attachment doesn't work if it is the only item in an array??? if (is_array($options['post_type']) && count($options['post_type']) == 1) { $options['post_type'] = $options['post_type'][0]; } // create tax queries if (!in_array('all', $options['taxonomy'])) { // vars $taxonomies = array(); $options['tax_query'] = array(); foreach ($options['taxonomy'] as $v) { // find term (find taxonomy!) // $term = array( 0 => $taxonomy, 1 => $term_id ) $term = explode(':', $v); // validate if (!is_array($term) || !isset($term[1])) { continue; } // add to tax array $taxonomies[$term[0]][] = $term[1]; } // now create the tax queries foreach ($taxonomies as $k => $v) { $options['tax_query'][] = array('taxonomy' => $k, 'field' => 'id', 'terms' => $v); } } unset($options['taxonomy']); // search if ($options['s']) { $options['like_title'] = $options['s']; add_filter('posts_where', array($this, 'posts_where'), 10, 2); } unset($options['s']); // load field $field = array(); if ($options['ancestor']) { $ancestor = apply_filters('acf/load_field', array(), $options['ancestor']); $field = acf_get_child_field_from_parent_field($options['field_key'], $ancestor); } else { $field = apply_filters('acf/load_field', array(), $options['field_key']); } // get the post from which this field is rendered on $the_post = get_post($options['post_id']); // filters $options = apply_filters('acf/fields/relationship/query', $options, $field, $the_post); $options = apply_filters('acf/fields/relationship/query/name=' . $field['_name'], $options, $field, $the_post); $options = apply_filters('acf/fields/relationship/query/key=' . $field['key'], $options, $field, $the_post); // query $wp_query = new WP_Query($options); // global global $post; // loop while ($wp_query->have_posts()) { $wp_query->the_post(); // right aligned info $title = '<span class="relationship-item-info">'; if (in_array('post_type', $field['result_elements'])) { $post_type_object = get_post_type_object(get_post_type()); $title .= $post_type_object->labels->singular_name; } // WPML if ($options['lang']) { $title .= ' (' . $options['lang'] . ')'; } $title .= '</span>'; // featured_image if (in_array('featured_image', $field['result_elements'])) { $image = get_the_post_thumbnail(get_the_ID(), array(21, 21)); $title .= '<div class="result-thumbnail">' . $image . '</div>'; } // title $title .= get_the_title(); // status if (get_post_status() != "publish") { $title .= ' (' . get_post_status() . ')'; } // filters $title = apply_filters('acf/fields/relationship/result', $title, $post, $field, $the_post); $title = apply_filters('acf/fields/relationship/result/name=' . $field['_name'], $title, $post, $field, $the_post); $title = apply_filters('acf/fields/relationship/result/key=' . $field['key'], $title, $post, $field, $the_post); // update html $r['html'] .= '<li><a href="' . get_permalink() . '" data-post_id="' . get_the_ID() . '">' . $title . '<span class="acf-button-add"></span></a></li>'; } if ((int) $options['paged'] >= $wp_query->max_num_pages) { $r['next_page_exists'] = 0; } wp_reset_postdata(); // return JSON echo json_encode($r); die; }
function acf_get_child_field_from_parent_field($child_name, $parent) { // vars $return = false; // find child if (isset($parent['sub_fields']) && is_array($parent['sub_fields'])) { foreach ($parent['sub_fields'] as $child) { if ($child['name'] == $child_name || $child['key'] == $child_name) { $return = $child; break; } // perhaps child has grand children? $grand_child = acf_get_child_field_from_parent_field($child_name, $child); if ($grand_child) { $return = $grand_child; break; } } } elseif (isset($parent['layouts']) && is_array($parent['layouts'])) { foreach ($parent['layouts'] as $layout) { $child = acf_get_child_field_from_parent_field($child_name, $layout); if ($child) { $return = $child; break; } } } // return return $return; }
function query_posts() { // vars $r = array('next_page_exists' => 1, 'html' => ''); // options $options = array('post_type' => 'all', 'taxonomy' => 'all', 'posts_per_page' => 10, 'paged' => 1, 'orderby' => 'title', 'order' => 'ASC', 'post_status' => 'any', 'suppress_filters' => false, 's' => '', 'lang' => false, 'update_post_meta_cache' => false, 'field_key' => '', 'nonce' => '', 'ancestor' => false); $options = array_merge($options, $_POST); // validate if (!wp_verify_nonce($options['nonce'], 'acf_nonce')) { die; } // WPML if ($options['lang']) { global $sitepress; if (!empty($sitepress)) { $sitepress->switch_lang($options['lang']); } } // convert types $options['post_type'] = explode(',', $options['post_type']); $options['taxonomy'] = explode(',', $options['taxonomy']); // load all post types by default if (in_array('all', $options['post_type'])) { $options['post_type'] = apply_filters('acf/get_post_types', array()); } // attachment doesn't work if it is the only item in an array??? if (is_array($options['post_type']) && count($options['post_type']) == 1) { $options['post_type'] = $options['post_type'][0]; } // create tax queries if (!in_array('all', $options['taxonomy'])) { // vars $taxonomies = array(); $options['tax_query'] = array(); foreach ($options['taxonomy'] as $v) { // find term (find taxonomy!) // $term = array( 0 => $taxonomy, 1 => $term_id ) $term = explode(':', $v); // validate if (!is_array($term) || !isset($term[1])) { continue; } // add to tax array $taxonomies[$term[0]][] = $term[1]; } // now create the tax queries foreach ($taxonomies as $k => $v) { $options['tax_query'][] = array('taxonomy' => $k, 'field' => 'id', 'terms' => $v); } } unset($options['taxonomy']); // load field $field = array(); if ($options['ancestor']) { $ancestor = apply_filters('acf/load_field', array(), $options['ancestor']); $field = acf_get_child_field_from_parent_field($options['field_key'], $ancestor); } else { $field = apply_filters('acf/load_field', array(), $options['field_key']); } // get the post from which this field is rendered on $the_post = get_post($options['post_id']); // filters $options = apply_filters('acf/fields/relationship/query', $options, $field, $the_post); $options = apply_filters('acf/fields/relationship/query/name=' . $field['_name'], $options, $field, $the_post); $options = apply_filters('acf/fields/relationship/query/key=' . $field['key'], $options, $field, $the_post); // query $wp_query = new WP_Query($options); // global global $post; // loop while ($wp_query->have_posts()) { $wp_query->the_post(); // get title $title = $this->get_result($post, $field, $the_post, $options); // update html $r['html'] .= '<li><a href="' . esc_url(get_permalink($post->ID)) . '" data-post_id="' . esc_attr($post->ID) . '">' . wp_kses_post($title) . '<span class="acf-button-add"></span></a></li>'; } // next page if ((int) $options['paged'] >= $wp_query->max_num_pages) { $r['next_page_exists'] = 0; } // reset wp_reset_postdata(); // return JSON echo json_encode($r); die; }
function get_sub_field_object($child_name) { // no field? if (empty($GLOBALS['acf_field'])) { return false; } // vars $depth = count($GLOBALS['acf_field']) - 1; $parent = $GLOBALS['acf_field'][$depth]['field']; // return return acf_get_child_field_from_parent_field($child_name, $parent); }
function query_posts() { // vars $options = array('post_type' => 'all', 'taxonomy' => 'all', 'posts_per_page' => 10, 'paged' => 0, 'orderby' => 'title', 'order' => 'ASC', 'post_status' => array('publish', 'private', 'draft', 'inherit', 'future'), 'suppress_filters' => false, 's' => '', 'lang' => false, 'update_post_meta_cache' => false, 'field_key' => '', 'nonce' => '', 'ancestor' => false); $options = array_merge($options, $_POST); // validate if (!wp_verify_nonce($options['nonce'], 'acf_nonce')) { die(0); } // WPML if ($options['lang']) { global $sitepress; $sitepress->switch_lang($options['lang']); } // convert types $options['post_type'] = explode(',', $options['post_type']); $options['taxonomy'] = explode(',', $options['taxonomy']); // load all post types by default if (in_array('all', $options['post_type'])) { $options['post_type'] = apply_filters('acf/get_post_types', array()); } // attachment doesn't work if it is the only item in an array??? if (is_array($options['post_type']) && count($options['post_type']) == 1) { $options['post_type'] = $options['post_type'][0]; } // create tax queries if (!in_array('all', $options['taxonomy'])) { // vars $taxonomies = array(); $options['tax_query'] = array(); foreach ($options['taxonomy'] as $v) { // find term (find taxonomy!) // $term = array( 0 => $taxonomy, 1 => $term_id ) $term = explode(':', $v); // validate if (!is_array($term) || !isset($term[1])) { continue; } // add to tax array $taxonomies[$term[0]][] = $term[1]; } // now create the tax queries foreach ($taxonomies as $k => $v) { $options['tax_query'][] = array('taxonomy' => $k, 'field' => 'id', 'terms' => $v); } } unset($options['taxonomy']); // search if ($options['s']) { $options['like_title'] = $options['s']; add_filter('posts_where', array($this, 'posts_where'), 10, 2); } unset($options['s']); // load field $field = array(); if ($options['ancestor']) { $ancestor = apply_filters('acf/load_field', array(), $options['ancestor']); $field = acf_get_child_field_from_parent_field($options['field_key'], $ancestor); } else { $field = apply_filters('acf/load_field', array(), $options['field_key']); } // get the post from which this field is rendered on $the_post = get_post($options['post_id']); // filters $options = apply_filters('acf/fields/relationship/query', $options, $field, $the_post); $options = apply_filters('acf/fields/relationship/query/name=' . $field['name'], $options, $field, $the_post); $options = apply_filters('acf/fields/relationship/query/key=' . $field['key'], $options, $field, $the_post); $results = ''; // load the posts $posts = get_posts($options); if ($posts) { foreach ($posts as $p) { // right aligned info $title = '<span class="relationship-item-info">'; if (in_array('post_type', $field['result_elements'])) { $title .= $p->post_type; } // WPML if ($options['lang']) { $title .= ' (' . $options['lang'] . ')'; } $title .= '</span>'; // featured_image if (in_array('featured_image', $field['result_elements'])) { $image = get_the_post_thumbnail($p->ID, array(21, 21)); $title .= '<div class="result-thumbnail">' . $image . '</div>'; } // find title. Could use get_the_title, but that uses get_post(), so I think this uses less Memory $title .= apply_filters('the_title', $p->post_title, $p->ID); // status if ($p->post_status != "publish") { $title .= " ({$p->post_status})"; } // filters $title = apply_filters('acf/fields/relationship/result', $title, $p, $field, $the_post); $title = apply_filters('acf/fields/relationship/result/name=' . $field['name'], $title, $p, $field, $the_post); $title = apply_filters('acf/fields/relationship/result/key=' . $field['key'], $title, $p, $field, $the_post); $results .= '<li><a href="' . get_permalink($p->ID) . '" data-post_id="' . $p->ID . '">' . $title . '<span class="acf-button-add"></span></a></li>'; } } echo $results; die; }