You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

WordPress自定义文章类型Ajax分类法筛选新增文章不显示问题

WordPress自定义文章类型迁移后Ajax筛选不显示新增文章问题

更新:我已经自行解决了当前问题。如果有朋友愿意帮我给接口返回结果添加分页功能,我愿意请你喝一杯你喜欢的饮品:) 此处部分代码需要清理,我会在后续处理。

我此前有一个名为insights的自定义文章类型,内含45篇文章,筛选功能运行正常。之后我使用CPT UI工具将所有文章迁移到了名为insight的自定义文章类型下,前期一切功能看起来都正常运行。我的筛选功能共使用5个自定义分类法构建Ajax筛选网格。

现在出现的问题是:所有新创建的文章在普通WP_Query查询中可以正常显示,但提交筛选表单后不会出现在返回结果中,比如第46、47篇文章就不会出现在筛选结果里,但所有旧文章都能正常筛选展示。我暂时找不到问题原因,怀疑是否是文章类型迁移导致的?下面是我使用的相关代码,感谢大家提供排查思路。我愿意付费寻求相关指导,提前感谢大家的帮助。


相关代码

1. functions.php 文件代码

global $wp_query;
wp_localize_script( 'ajax-pagination', 'ajaxpagination', array(
    'ajaxurl' => admin_url( 'admin-ajax.php' ),
    'query_vars' => json_encode( $wp_query->query )
));

require_once( get_template_directory() . '/library/functions/filter-scripts.php'); 
require_once( get_template_directory() . '/library/functions/ajax-filter-insights.php');

2. /library/functions/filter-scripts.php 文件代码

<?php
/*
* Enqueue scripts.js if file scripts.js exists
*/
function load_scripts() {

    wp_enqueue_script('ajax', get_template_directory_uri() . '/assets/js/filter-scripts.js', array('jquery'), NULL, true);

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

}

add_action( 'wp_enqueue_scripts', 'load_scripts' );

3. /assets/js/filter-scripts.js 文件代码

(function ($) {

  $(document).ready(function () {

    $(document).on('submit', '[data-js-form=filter]', function (e) {

      e.preventDefault();
      var data = $(this).serialize();

      $.ajax({
        url: wpAjax.ajaxUrl,
        data: data,
        type: 'post',
        success: function (result) {
          $('[data-js-filter=target]').html(result);
        },
        error: function (result) {
          console.warn(result);
        }
      });


    });
  });

})(jQuery);

4. /library/functions/ajax-filter-insights.php 文件代码

<?php 
add_action('wp_ajax_myfilter', 'misha_filter_function'); // wp_ajax_{ACTION HERE}
add_action('wp_ajax_nopriv_myfilter', 'misha_filter_function');

function misha_filter_function(){
    $args = array(
        'post_type' => 'insight',
        'post_status' => 'published'
    );

    if( isset( $_POST['all-services'] ) ) {
        $args['tax_query'][] = array(
            'taxonomy' => 'insight_service',
            'field' => 'id',
            'terms' => $_POST['insight-service']
        );
    }

    if( isset( $_POST['all-types'] ) ) {
        $args['tax_query'][] = array(
            'taxonomy' => 'insight_types',
            'field' => 'id',
            'terms' => $_POST['insight-types']
        );
    }
    if( isset( $_POST['all-topics'] ) ) {
        $args['tax_query'][] = array(
            'taxonomy' => 'insight_topics',
            'field' => 'id',
            'terms' => $_POST['insight-topics']
        );
    }
    if( isset( $_POST['all-months'] ) ) {
        $args['tax_query'][] = array(
            'taxonomy' => 'insight_months',
            'field' => 'id',
            'terms' => $_POST['insight-months']
        );
    }
    if( isset( $_POST['all-years'] ) ) {
        $args['tax_query'][] = array(
            'taxonomy' => 'insight_years',
            'field' => 'id',
            'terms' => $_POST['insight-years']
        );
    }

    if( isset( $_POST['insight-service'] ) ) {
        $args['tax_query'][] = array(
            'taxonomy' => 'insight_service',
            'field' => 'id',
            'terms' => $_POST['insight-service']
        );
    }

    if( isset( $_POST['insight-type'] ) ) {
        $args['tax_query'][] = array(
            'taxonomy' => 'insight_type',
            'field' => 'id',
            'terms' => $_POST['insight-type']
        );
    }

    if( isset( $_POST['insight-topic'] ) ) {
        $args['tax_query'][] = array(
            'taxonomy' => 'insight_topic',
            'field' => 'id',
            'terms' => $_POST['insight-topic']
        );
    }
    if( isset( $_POST['insight-month'] ) ) {
        $args['tax_query'][] = array(
            'taxonomy' => 'insight_month',
            'field' => 'id',
            'terms' => $_POST['insight-month']
        );
    }
    if( isset( $_POST['insight-year'] ) ) {
        $args['tax_query'][] = array(
            'taxonomy' => 'insight_year',
            'field' => 'id',
            'terms' => $_POST['insight-year']
        );
    }

    $query = new WP_Query( $args );

    if( $query->have_posts() ) : ?>
<style>
    .post-grid__col {
        display: none;
    }
</style>
<div class="container-fluid post-grid p--normal">
    <div class="row my-3">
        <?php
                while( $query->have_posts() ): $query->the_post(); ?>


        <div class="post-grid__col col-12 col-md-6 col-lg-4 m-0">
            <a href="<?php echo the_permalink();?>">
                <div class="post-card h-100 animate__animated animate__fadeInUp">
                    <div class="post-card__thumb"
                        style="background: url(<?php echo get_the_post_thumbnail_url();?>) no-repeat; background-size: 100%; background-position: center;">
                        <?php include (TEMPLATEPATH . '/includes/general/post-tax-term.php'); ?>

                    </div>
                    <p class="text--lightblue text-uppercase letter-spacing--261 font-53-ex"> <?php echo get_the_date();?>
                    </p>

                    <h2 class="post-card__title">
                        <?php echo the_title();?>
                    </h2>
                </div>
            </a>

        </div>
        <?php
                endwhile;

                wp_reset_postdata();
                ?>

        <div class="col-12">
            <div class="load-btn__wrapper d-flex justify-content-between align-items-center">
                <a href="#" id="loadMore" class="cta-btn">
                    <div class="cta-inner cta-inner--inverted d-flex align-items-center"><span
                            class="arrow_carrot-right text-white"></span><span class="btn-label">Show More</span></div>
                </a>

                <p class="totop">
                    <a href="#filter" class="no-transform">Back to top &#9650;</a>
                </p>
            </div>
        </div>
        <script>
            jQuery(function ($) {

                $(function () {
                    $(".post-grid__col").slice(0, 9).show();
                    $("#loadMore").on('click', function (e) {
                        e.preventDefault();
                        $(".post-grid__col:hidden").slice(0, 9).fadeIn();
                        if ($(".post-grid__col:hidden").length < 9) {
                            $("#loadMore").fadeOut('slow');
                        }

                    });
                });

                $("a[href='#filter']").click(function () {
                    $("html, body").animate({
                        scrollTop: $('#filter').offset().top - 140
                    }, "slow");
                    return false;
                });

                $(window).scroll(function () {
                    if ($(this).scrollTop() > 50) {
                        $('.totop a').fadeIn();
                    } else {
                        $('.totop a').fadeOut();
                    }

                });
            });
        </script>
        <?php
            else :
                echo '<div class="post-grid__col col-12 mt-5 pt-5 no-results-found">
                <div class="post-card h-100 animate__animated animate__fadeInUp border-right-0">
                   <p class="display-4">No results found.</p>
                </div>
        </div>';
            endif;

    die();
}

5. page-insights.php 文件

暂未提供该文件内容,文件包含筛选表单和页面初始查询逻辑。


内容的提问来源于stack exchange,提问作者Jim Poulos

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.04 04:21:00