搜索过滤器CPT,自定义循环

时间:2020-06-09 作者:Kathleen Haynes

我有一个自定义循环,可以在Wordpress站点的网格中显示项目。我正在为这些项目创建过滤器,并且在输入搜索上陷入了困境。我的管理ajax出现400错误。php和不确定如何使其工作。有人能给我一些提示吗?这是我的代码:

PHP code for loop in functions.php

add_action(\'wp_ajax_my_action\' , \'data_fetch\');
add_action(\'wp_ajax_nopriv_my_action\',\'data_fetch\');

function data_fetch(){
$args = array(
        \'orderby\' => \'title\',
        \'order\' => \'ASC\',
        \'post_type\' => \'projects\',
        \'posts_per_page\' => -1,
        \'s\' => esc_attr( $_POST[\'keyword\'] ),
    ); 

    $the_query = new WP_Query($args); 

    if( $the_query->have_posts() ) : while ($the_query->have_posts() ) : $the_query->the_post(); 

    ?>


    <div class="projects-column">
    <div class="project-image">
    <?php $image = get_field(\'image\');
    if( !empty($image) ): ?>
    <img src="<?php echo $image[\'url\']; ?>" class="project-image-tester" alt="<?php echo $image[\'alt\']; ?>" />
    <?php endif; ?>
    <div class="project-info">
    <span class="proj-name" style="font-weight: bold;"><b><?php the_title(); ?></b></span>
    </div>


    <div class="project-onhover">
    <span class="proj-title"><p><b><?php the_title(); ?></b></p></span>
    <span class="proj-details">
    <p><span style="font-weight: bold;">LOCATION: </span><?php the_field(\'location\'); ?></p>
    <p><span style="font-weight: bold;">PROJECT VALUE: </span><?php the_field(\'project_value\'); ?></p>
    <p><span style="font-weight: bold;">SERVICES: </span> <?php echo wp_strip_all_tags( get_the_term_list( $post->ID, \'project_services\', \'\', \', \', \'\' ) ); ?> </p></span>
    </div><!--projects-on-hover-tester -->
    </div><!--project-image-->
    </div><!--projects-column -->


    <?php 
    endwhile;
    wp_reset_postdata();
    php endif;
    die();
}

JS code also in functions.php

// add the ajax fetch js
add_action( \'wp_footer\', \'ajax_fetch\' );
function ajax_fetch() {
?>
<script type="text/javascript">
function fetch(){

    jQuery.ajax({
        url: wpAjax.ajaxUrl,
        type: \'post\',
        data: { action: \'data_fetch\', keyword: jQuery(\'#keyword\').val() },
        success: function(data) {
            jQuery(\'#projects-container\').html( data );
        }
    });

}
</script>

<?php
}
我之前为另一个ajax请求对脚本进行了本地化,因此url的来源是:

wp_localize_script(\'ajax\' , \'wpAjax\',
    array(\'ajaxUrl\' => admin_url(\'admin-ajax.php\'))

1 个回复
SO网友:Jacob Peattie

您为挂钩使用了错误的名称。As documented here, 挂钩名称应为wp_ajax_ 然后是action 您在请求中发送的参数。在你的例子中data_fetch, 因此:

add_action(\'wp_ajax_my_action\' , \'data_fetch\');
add_action(\'wp_ajax_nopriv_my_action\',\'data_fetch\');
应为:

add_action( \'wp_ajax_data_fetch\', \'data_fetch\' );
add_action( \'wp_ajax_nopriv_data_fetch\', \'data_fetch\' );

相关推荐

Get Current User ID Inside a Loop返回0表示短码

我有一个函数,它对给定作者的所有帖子都有一个循环。我使用get_current_user_id() 但这在循环中似乎不起作用,或者可能是它的短代码问题。我的函数在shortcode的帮助下运行。当前用户总是返回0,因此它会显示我网站上所有帖子的元数据。function get_meta_value_by_meta_key(){ $author_id = \'get_current_user_id()\'; // do stuff to get user I $author