WordPress Ajax Sempre retornando 0

Estou tentando obter mais postagens no clique via ajax. Nas minhas funções, localizei o script

wp_enqueue_script( 'news', get_template_directory_uri().'/news/js/news.js', '', '', true ); //Localise script for ajax call wp_localize_script( 'news', 'ajax_posts', array( 'ajaxurl' => admin_url( 'admin-ajax.php' ), 'noposts' => 'No Older Posts Found', )); 

Minha function para consultar postagens

 function more_post_ajax(){ $ppp = (isset($_POST["ppp"])) ? $_POST["ppp"] : 2; $page = (isset($_POST['pageNumber'])) ? $_POST['pageNumber'] : 0; header("Content-Type: text/html"); $args = array( 'suppress_filters' => true, 'post_type' => 'post', 'posts_per_page' => $ppp, 'paged' => $page ); $loop = new WP_Query($args); $out = ''; if ($loop -> have_posts()) : while ($loop -> have_posts()) : $loop -> the_post(); $out .='
'.get_the_post_thumbnail().'

'.the_title().'

Date: '.get_the_date().'
Author: '.the_author().'
'.get_custom_excerpt(get_the_content()).' Read More
'; endwhile; endif; wp_reset_postdata(); echo $out; } add_action('wp_ajax_nopriv_more_post_ajax', 'more_post_ajax'); add_action('wp_ajax_more_post_ajax', 'more_post_ajax');

e então meu jQuery

 jQuery(document).ready(function(){ var ppp = 2; // Post per page var pageNumber = 1; function load_posts(){ pageNumber++; var str = '&pageNumber=' + pageNumber + '&ppp=' + ppp + '&action=more_post_ajax'; jQuery.ajax({ type: "POST", dataType: "html", url: ajax_posts.ajaxurl, data: str, success: function(data){ console.log(data); var $data = jQuery(data); if($data.length){ jQuery(".posts-wrapper").append($data); jQuery(".load-more").attr("disabled",false); } else{ jQuery(".load-more").attr("disabled",true); } }, error : function(jqXHR, textStatus, errorThrown) { $loader.html(jqXHR + " :: " + textStatus + " :: " + errorThrown); } }); return false; } jQuery(".load-more").on("click",function(e){ // When btn is pressed. jQuery(".load-more").attr("disabled",true); // Disable the button, temp. load_posts(); e.preventDefault(); }); }); 

A resposta ajax é sempre 0. Alguém poderia ajudar por favor.

Solutions Collecting From Web of "WordPress Ajax Sempre retornando 0"

Experimente a function die () no final da function PHP. Isso ajudará.

UPD: Eu ve made some simple version of your problem code to check it and it works. I think it will helps you. You are able to change inner PHP script to your logic and it won ve made some simple version of your problem code to check it and it works. I think it will helps you. You are able to change inner PHP script to your logic and it won ve made some simple version of your problem code to check it and it works. I think it will helps you. You are able to change inner PHP script to your logic and it won t retorna 0.

PHP:

 function more_post_ajax(){ echo "Hello"; die(); } add_action('wp_ajax_nopriv_more_post_ajax', 'more_post_ajax'); add_action('wp_ajax_more_post_ajax', 'more_post_ajax'); function custom_scripts_init(){ wp_enqueue_script( 'custom', get_template_directory_uri().'/assets/js/custom.js', array('jquery') ); wp_localize_script( 'custom', 'ajaxPosts', array( 'customUrl' => admin_url( 'admin-ajax.php' ), 'noposts' => 'No Older Posts Found', )); } add_action('wp_footer', 'custom_scripts_init'); 

JQuery:

 jQuery(document).ready(function(){ jQuery("#page").append(""); var ppp = 2; // Post per page var pageNumber = 1; function load_posts(){ pageNumber++; var str = '&pageNumber=' + pageNumber + '&ppp=' + ppp + '&action=more_post_ajax'; jQuery.ajax({ type: "POST", dataType: "html", url: ajaxPosts.customUrl, data: str, success: function(data){ console.log(data); }, error : function(jqXHR, textStatus, errorThrown) { //$loader.html(jqXHR + " :: " + textStatus + " :: " + errorThrown); } }); return false; } jQuery(".load-more").on("click",function(e){ // When btn is pressed. // jQuery(".load-more").attr("disabled",true); // Disable the button, temp. load_posts(); e.preventDefault(); }); 

});

No caso de alguém chegar a isso, eu apenas pensei em acrescentar qual era o problema.

Eu herdei o trabalho de outro desenvolvedor e, por razões que nunca entender, houve ob_start na parte superior do arquivo de funções. Isso fazia com que o pedido ajax falhasse.

obrigado