AJAX在未登录WordPress时停止工作

时间:2020-09-26 作者:mohammad

如果用户登录,我的代码工作正常,但注销时不工作

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();
}





 ?>

1 个回复
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() ) :

也许会触发一些钩子来查找客人。我检查过了-您的代码正在运行

相关推荐

在提交AJAX表单时打开模式弹出窗口

我正在将一个表单从旧网站迁移到新网站,但是在提交AJAX表单之后应该打开的jQuery模式不起作用。你可以在这里测试https://tomlab.dreamhosters.com/sign-the-declaration/表单似乎正在运行,但弹出窗口未显示,但如果我访问https://tomlab.dreamhosters.com/sign-the-declaration/#modal 笔直,打开正确。希望你能帮助我!我已经为此奋斗了几天了。谢谢以下是功能代码:function forms_sumate_