0
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();
}
  • $value = get_post_meta($post_id, 'repeater_1_sub_field', true); – Vishwa May 8 at 10:46
  • In $args you should have 'meta_query' => array( array('key' => 'nimi', ...) ) – nmr May 8 at 10:49
  • @nmr I tried with meta_query, but it didn't show any data with that. – user167617 May 8 at 12:24
  • @Vishwa Where should i put that code into and how does it help? – user167617 May 8 at 12:25
  • @user167617 try at here $name = get_sub_field('nimi'); – Vishwa May 9 at 4:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.