ACF过滤器自定义帖子由中继器字段的子字段

问题描述:

我正在关注ACF文档中的官方guide,但未能正确完成。我正在使用高级自定义字段和自定义帖子类型UI插件。ACF过滤器自定义帖子由中继器字段的子字段

我有一个名为材料一个自定义后类型,每种材料都有一个文件中继场,子场之一冠军。我想根据标题查询帖子,并使用ajax将结果放到页面上。

这里是我的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'); 

这里是我的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); 

阿贾克斯和查询工作的罚款,如果我查询所有的材料后不进行过滤标题所以只能认为这是查询本身是错误的。我遵循指南,但坚持了几个小时。

+0

我知道您希望您的查询检索与repeater文件上的任何行匹配子字段title的所有'materials',对吧? –

+0

是的,这正是我想要的@JordiNebot – YaphatS

我想你唯一的错误是在meta_query本身。除了(可选的)第一级relation之外,meta_query必须是阵列的阵列。尝试:

$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, 
     ) 
    ) 
); 

WP Codex

meta_query(阵列) - 包含一个或多个阵列使用下列按键:[...]

我复制你的情况(除为Ajax)和查询工作正常,所以我想这也应该通过Ajax调用。