• I want to get custom post type list by ajax code, but it always appearance “0” on html.
    here is my code:

    <div class="daily_select">
              <label> Ch?n T?nh Thành </label>
              <?php  wp_dropdown_categories( array(
    	'show_option_all'    => '',
      'hierarchical'       => 1,
      'taxonomy'           => 'daily_cat',
      'hide_if_empty'      => 0,
      'name'               => 'cat',
    	'id'                 => 'daily_cat',)); ?>
                </div>
              <script type="text/javascript">
                    (function($){
                        $("#daily_cat").change(function(){
                            var sub_id = $("#daily_cat option:selected").val();
                  									$("#daily_listitem").empty();
                            $("#loading").fadeIn('slow');
                        	$.ajax({
                        		type: "post",
                                url: "<?php echo admin_url( 'admin-ajax.php' ); ?>",
                                data: { action: 'get_daily', sub_id: sub_id },
                        		beforeSend: function() {$("#loading").fadeIn('slow');},
                        		success: function(data) {
                                    $("#loading").fadeOut('slow');
                                    $("#daily_listitem").append(data);
                        		}
                        	});
                        });
                    })(jQuery);
                </script>
        <div  id="loading" style="display: none;text-align:center;color:red">Loading...</div>
              <div id="daily_listitem">
     </div>

    And this is my function

    function get_daily()
            	{
                if(!isset($_POST['sub_id'])){$_POST['sub_id']=21;}
                $args = array(
                  'post_type' => 'daily',
                  'tax_query' => array(
            array(
                'taxonomy' => 'daily_cat',
                'field' => 'term_id',
                'terms' =>  $_POST['sub_id']
            )
        )
    						); ?>
    <ul class="listitem">
    <?php
               $my_query = new WP_Query( $args );
                    if( $my_query->have_posts() ) : while( $my_query->have_posts() ) : $my_query->the_post();?>
            <li class="daily-item" data-map="<?php the_field('test'); ?>">
                  <h5><?php the_title(); ?></h5>
              <div class="daily-item-body"><?php the_content(); ?></div>
                  </li>
             <?php  endwhile; endif; ?></ul>
          <?php  	}
      add_action( 'wp_ajax_get_daily', 'get_daily' );
      add_action('wp_ajax_nopriv_get_daily', 'get_daily');

    Thank all !

    [Moderator note: code fixed. Please wrap code in the backtick character or use the code button.

    Only images from certain white listed sites will appear here. The URL must end in a proper image filename, thus URLs from Google Images will not work. I suggest you post images on imgur.com if you want them to appear here]

    • This topic was modified 7 years, 7 months ago by aminor1993.
    • This topic was modified 7 years, 7 months ago by bcworkz. Reason: broken code, bad image links
Viewing 2 replies - 1 through 2 (of 2 total)
  • Moderator bcworkz

    (@bcworkz)

    I don’t think your query is returning anything. Change the default sub_id value to that which should be sent from jQuery based on the selected option, then call your get_daily() function from a template file. Load something that uses that template. Is anything output from this query?

    If you get what’s expected that way, it seems then that jQuery isn’t getting the right value from the selected option.

    I don’t really see anything wrong with your code, so what’s left to do is generate debug output to confirm every line is doing what it should be doing. Debugging Ajax handlers is difficult since the browser is not expecting output. You have to send the debug output somewhere, the error log often works. If there’s too much output for error log, write the output to your own log file.

    It can be tedious work, but you will eventually find the problem.

    Thread Starter aminor1993

    (@aminor1993)

    Yeah, it work fine !. It get post when choose category to form dropdown
    But it always appearance “0”
    image1
    image 2

    • This reply was modified 7 years, 7 months ago by aminor1993.
Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Ajax load custom post type but always appearance “0”’ is closed to new replies.