dpepbjp126917 2019-05-07 14:06
浏览 33

基于Ajax搜索字段的自定义查询,显示所有acf字段值

I have a query, which gets the results i want, but when i try compare it with 'LIKE' it still shows all results.

Example. I have ACF fields 'Test1' and 'Test2'. If i type 'xfag' in the search field both results will appear anyway.

I already tried to use meta_query but then it didn't show any data.

So this is the query im having problems with:

$args = array(
    'post_type' => 'page',
    'posts_per_page' => -1,
    array(
      'key'     => 'nimi',
      'value'   => $_POST['keyword'],
      'compare' => 'LIKE',
    )
  );

All of the code:

add_action('wp_footer', 'ajax_fetch');
function ajax_fetch()
{ ?>

  <script type="text/javascript">
    function fetch() {

      jQuery.ajax({
        url: '<?php echo admin_url('admin-ajax.php'); ?>',
        type: 'post',
        data: {
          action: 'data_fetch',
          keyword: jQuery('#keyword').val()
        },
        success: function(data) {
          jQuery('#datafetch').html(data);
        }
      });

    }
  </script>
<?php
}

// the ajax function
add_action('wp_ajax_data_fetch', 'data_fetch');
add_action('wp_ajax_nopriv_data_fetch', 'data_fetch');

function data_fetch()
{
  if (esc_attr($_POST['keyword']) == null) {
    die();
  }

  $args = array(
    'post_type' => 'page',
    'posts_per_page' => -1,
    array(
      'key'     => 'nimi',
      'value'   => $_POST['keyword'],
      'compare' => 'LIKE',
    )
  );
  $the_query = new WP_Query($args);

  if ($the_query->have_posts()) :
    while ($the_query->have_posts()) : $the_query->the_post(); ?>

      <?php if (have_rows('tuotteet')) : ?>

        <?php while (have_rows('tuotteet')) : the_row(); ?>

          <?php if (have_rows('alatuotteet')) : ?>

            <?php while (have_rows('alatuotteet')) : the_row(); ?>

              <?php
              $name = get_sub_field('nimi');
              echo $name; ?>

            <?php endwhile;
        endif; ?>
        <?php endwhile;
    endif; ?>

    <?php endwhile;
  wp_reset_postdata();
endif;

die();
}
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 虚幻5 UE美术毛发渲染
    • ¥15 CVRP 图论 物流运输优化
    • ¥15 Tableau online 嵌入ppt失败
    • ¥100 支付宝网页转账系统不识别账号
    • ¥15 基于单片机的靶位控制系统
    • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
    • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
    • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
    • ¥15 手机接入宽带网线,如何释放宽带全部速度
    • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测