posts_request、posts_results 和 the_posts 一次运行

posts_request, posts_results, and the_posts one time run

提问人:gencyazilimci 提问时间:11/17/2023 更新时间:11/17/2023 访问量:6

问:

在 WordPress 中,我正在使用常规站点搜索和 AJAX 执行站点搜索。在这些搜索之后,我想检索通过钩子进行的查询,以及找到的帖子和帖子类型等数据。我用过钩子 posts_request、posts_results 和 the_posts。但是,在通过 AJAX 进行的现场搜索中,它只工作一次,而在常规的现场搜索中,它工作两次。你能帮我解决这个问题吗?先谢谢你。

class My_Custom_Class {
    private static $isExecuted = false;

    public function __construct() {
        add_action('posts_results', array($this, 'my_custom_posts_results'), 10, 2);
    }

    public function my_custom_posts_results($posts, $query) {
        if (!self::$isExecuted) {
            if ($query->is_search()) {
                if ($query->found_posts > 0) {
                    // Actions to be performed
                    // ...
                }
                self::$isExecuted = true;
            }
        }

        return $posts;
    }
}

$my_custom_instance = new My_Custom_Class();
Ajax wordpress 搜索

评论


答: 暂无答案