【问题标题】:ACF filter custom posts by subfield of repeater fieldACF 按转发器字段的子字段过滤自定义帖子
【发布时间】:2017-05-10 16:51:11
【问题描述】:

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

我有一个名为 ma​​terials 的自定义帖子类型,每个材料都有一个 files 重复字段,其中一个子字段是 title。我想根据标题查询帖子,并使用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);

如果我在不过滤标题的情况下查询所有材料帖子,则 ajax 和查询工作正常,因此唯一认为错误的是查询本身。我按照指南进行操作,但被困了好几个小时。

【问题讨论】:

  • 我知道您希望您的查询检索与子字段 title 匹配的所有 materials 转发器 files 上的任何行,对吗?
  • 是的,这正是我想要的@JordiNebot

标签: mysql ajax wordpress custom-post-type advanced-custom-fields


【解决方案1】:

我猜你唯一的错误在于meta_query 本身。除了(可选的)第一级relationmeta_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 (array) - 包含一个或多个数组 具有以下键:[…]

我复制了您的案例(Ajax 除外)并且查询工作正常,所以我想这也应该适用于 Ajax 调用。

【讨论】:

    猜你喜欢
    • 2020-04-20
    • 2021-01-03
    • 2016-01-11
    • 2020-12-13
    • 2015-07-14
    • 1970-01-01
    • 1970-01-01
    • 2021-11-30
    • 2021-12-10
    相关资源
    最近更新 更多