Question: ACF filter custom posts by subfield of repeater field

Question

ACF filter custom posts by subfield of repeater field

Answers 1
Added at 2016-12-26 08:12
Tags
Question

I'm following the official guide in ACF documentation but hasn't been able to get it right. I'm using Advanced custom fields and Custom post type UI plugins.

I have a custom post type named materials, each material has a files repeater field, one of the subfield is title. I want to query the posts based on the title and put the results onto the page using ajax.

Here's my functions.php:

function materialsSearchAjax() {

  $html = "";
  $keyword = $_POST['keyword'];
  // args
  $args = array(
    'numberposts'   => -1,
    'posts_per_page' => -1,
    'post_type'     => 'materials',
    'meta_key'    => 'type',
    'meta_value'    => 'students',
    'meta_query'    =>
        array(
            'key'       => 'files_%_title',
            'compare'   => 'LIKE',
            'value'     => $keyword,
    )
  );

  $query = new WP_Query( $args );
  $posts = array();
  $html .= '<div class="Materials-students">';

  while( $query->have_posts() ) : $query->the_post();
    $html .= '<div class="Files-list u-padding-left--12">';
      if( have_rows('files') ){
        while ( have_rows('files') ) : the_row();
          $html .= '<div class="Files-item u-margin-right--30 u-margin-bottom--18">';
          $html .= '<div class="Files-itemImage"></div>';
          $html .= '<a href="' . the_sub_field("document") . '" target="_blank" class="Files-itemLink">';
          $html .= the_sub_field('title');
          $html .= '</a>';
          $html .= '</div>';
        endwhile;
      }
    $html .= '</div>';
  endwhile;

  $html .= '</div>';

  wp_reset_query();
  return $html;
}

// filter
function materials_where( $where ) {

    $where = str_replace("meta_key = 'files_%", "meta_key LIKE 'files_%", $where);

    return $where;
}

function igs_scripts_styles() {
  wp_enqueue_script( 'ajaxMaterialsSearch', get_template_directory_uri() . '/assets/scripts/ajaxMaterialsSearch.js', array(), false, true );
  wp_localize_script( 'ajaxMaterialsSearch', 'ajax_data_object', array( 'url' => admin_url( 'admin-ajax.php' )) );
}

add_action('wp_ajax_nopriv_materialsSearchAjax', 'materialsSearchAjax');
add_action('wp_ajax_materialsSearchAjax', 'materialsSearchAjax');
add_filter('posts_where', 'materials_where');
add_action('wp_enqueue_scripts', 'igs_scripts_styles');

Here's my ajax:

(function($) {
  // Trigger submit
  $('.Search-magnifier').on('click', function(){
    var $form = $(this).parent();
    $($form).submit();
  });

  $('.Search-form').on('submit', function(event){
    event.preventDefault();
    var $form = $(this);
    var searchKeyword = $($form).find('input[type="search"]').val();
    console.log('keyword: ' + searchKeyword);
    $.ajax({
      type: 'POST',
      url: ajax_data_object.url,
      data: {action: 'materialsSearchAjax', keyword: searchKeyword},
      success: function(textStatus) {
        // update the content
        console.log(textStatus);
        $('.Materials-students').replaceWith(textStatus);
      },
      error: function(XMLHttpRequest, textStatus, errorThrown) {
        console.log(errorThrown);
      }
    });
  });
})(jQuery);

The ajax and query work fine if I query all the materials post without filtering the title so the only think that's wrong is the query itself. I followed the guide but been stuck for hours.

Answers to

ACF filter custom posts by subfield of repeater field

nr: #1 dodano: 2016-12-28 17:12

I guess your only mistake is within the meta_query itself. Besides the (optional) first-level relation, a meta_query has to be an array of array(s). Try:

$args = array(
    'posts_per_page' => -1,
    'post_type'      => 'materials',
    'meta_key'       => 'type',
    'meta_value'     => 'students',
    'meta_query'     => array(
        array(
            'key'     => 'files_%_title',
            'compare' => 'LIKE',
            'value'   => $keyword,
        )
    )
);

From WP Codex:

meta_query (array) - Contains one or more arrays with the following keys: […]

I replicated your case (except for the Ajax) and the query worked fine, so I guess this should also work over Ajax calls.

Source Show
◀ Wstecz