如果用户登录,我的代码工作正常,但注销时不工作
functions.php
//ajax search --
// add the ajax fetch js
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(){
$the_query = new WP_Query( array( \'posts_per_page\' => 5, \'s\' => esc_attr( $_POST[\'keyword\'] ) ) );
if( $the_query->have_posts() ) :
while( $the_query->have_posts() ): $the_query->the_post(); ?>
<div class="post-box-search">
<a href="<?php echo esc_url( post_permalink() ); ?>">
<?php the_post_thumbnail( \'index_post\' ); ?>
<h3><?php the_title();?></h3>
</a>
</div>
<?php endwhile;
wp_reset_postdata();
endif;
die();
}
?>
SO网友:Valerii Vasyliev
尝试输出调试查询
查找片段
$the_query = new WP_Query( array( \'posts_per_page\' => 5, \'s\' => esc_attr( $_POST[\'keyword\'] ) ) );
if( $the_query->have_posts() ) :
替换为
$the_query = new WP_Query( array( \'posts_per_page\' => 5, \'s\' => esc_attr( $_POST[\'keyword\'] ) ) );
echo (string)$the_query->request;
if( $the_query->have_posts() ) :
也许会触发一些钩子来查找客人。我检查过了-您的代码正在运行