如何通过AJAX调用挂钩pre_get_post筛选器

时间:2020-01-30 作者:saravanan kanagaraj

我需要在wordpress网站的搜索页面上添加过滤器选项。创建了带有两个选项的下拉列表。一个是“最近添加”和“上次更新”。我们需要通过选择下拉菜单来筛选课程。

我创建了下拉列表并编写了jquery,以使用ajax调用方法钩住过滤器。已创建筛选器。php文件,并在此文件中钩住pre\\u get\\u posts方法。如果我们选择下拉列表,它需要挂接该过滤器,但它不会挂接。此外,我还尝试通过ajax调用传递参数,但不起作用。请分享实现过滤器选项的想法。

HTML

<select id="filter">
    <option value=\'last_updated_posts\' id=\'recently_added\'>
        Last Updated
    </option>
    <option value="recently_added_posts" id=\'last_updated\'>
        Recently Added
    </option>
</select>

Custom.js

  jQuery("#filter").change(function(){
      var selected_option=jQuery("#filter option:selected").val();
        jQuery.ajax({
            type: \'POST\',
            url: ajaxurl,
            data: {\'action\' :selected_option },
            success: function(response){alert(response);}
        });
    });

filter.php

<?php
add_action(\'wp_ajax_recently_added_posts\', \'recently_added_posts\');
add_action(\'wp_ajax_nopriv_recently_added_posts\', \'recently_added_posts\');
if($_POST[\'action\'] ==\'recently_added_posts\' ){
function recently_added_posts($query) {
     if ($query->is_search() && !is_admin() ) {
        $query->set(\'post_type\',array(\'sfwd-courses\',\'sfwd-lessons\',\'sfwd-topic\'));
        $query->set(\'orderby\',array(
        \'post_type\'=> \'ASC\',
        \'date\' => \'DESC\')
    );
    return $query;         
    die();
}
add_filter(\'pre_get_posts\',\'recently_added_posts\'); 
}
}
add_action(\'wp_ajax_last_updated_posts\', \'last_updated_posts\');
add_action(\'wp_ajax_nopriv_last_updated_posts\', \'last_updated_posts\');
if($_POST[\'action\'] ==\'last_updated_posts\' ){
function last_updated_posts($query) {
    if ($query->is_search() && !is_admin() ) {
        $query->set(\'post_type\',array(\'sfwd-courses\',\'sfwd-lessons\',\'sfwd-topic\'));
        $query->set(\'orderby\',array(
        \'post_type\'=> \'ASC\',
        \'modified\' => \'DESC\')
    );
    }
    return $query;
    die();
} 
add_filter(\'pre_get_posts\',\'last_updated_posts\');
}
function ajaxurl_filter() {
   echo \'<script type="text/javascript">
           var ajaxurl = "\' . admin_url(\'admin-ajax.php\') . \'";
         </script>\';
}
add_action(\'wp_head\', \'ajaxurl_filter\');
?>
提前感谢!

2 个回复
SO网友:Amin

您可以在ajax回调函数的WP\\U查询上应用自定义过滤器,如下所示:

$my_query = apply_filters(\'my_plugin_pre_get_posts\', (new WP_Query($args)));
现在您可以使用现有pre_get_posts 用于ajax调用的函数,

add_action(\'pre_get_posts\', \'myplugin_filter_posts\'); // For All WP_Query instances
add_filter(\'my_plugin_pre_get_posts\', \'myplugin_filter_posts\'); // For ajax

function myplugin_filter_posts($query) {
    if (defined(\'DOING_AJAX\') && DOING_AJAX) {
        // Your code executed only on ajax requests
    }

    // Codes here Execute everywhere

    return $query; // Return $query is required for the filter we defined
}
你可以调整它来实现你的愿望

SO网友:Waldo Rabie

您实际上没有执行新的WP\\U查询,因此不会返回任何结果。试试这个。。。

<?php
add_action(\'wp_ajax_recently_added_posts\', \'recently_added_posts\');

if ($_POST[\'action\'] ==\'recently_added_posts\' ) {
    function recently_added_posts( $query ) {
        // Setup query
        global $wp_query;

        // Define arguments
        $query_args = array( 
            \'post_type\' => array( 
                \'sfwd-courses\', 
                \'sfwd-lessons\', 
                \'sfwd-topic\' ),
            \'orderby\' => array(
                \'post_type\'=> \'ASC\',
                \'date\' => \'DESC\'
            ),
            \'posts_per_page\' => 10, // (change if needed)

        );

        $wp_query = new WP_Query($query_args);

        // You can then loop through the posts in your themes template file
        // NOTE: may need to play around this but if you just return the query you\'ll see all the posts in the front end
        $_template_file = THEME_DIR . \'search.php\';
        load_template( $_template_file, $require_once = true );

        die();
    }

    // This will now work because the query was instantiated...
    add_filter(\'pre_get_posts\',\'recently_added_posts\'); 
}
?>

相关推荐

使用新的WP-Query()从循环中过滤后期格式;

嗨,我目前正在为我的博客构建一个主题。下面的代码指向最新的帖子(特色帖子)。因为这将有一个不同的风格比所有其他职位。然而我想过滤掉帖子格式:链接使用我在循环中定义的WP查询,因为它给我带来了更多的灵活性。我该怎么做呢? <?php $featured = new WP_Query(); $featured->query(\'showposts=1\'); ?> <?php while ($featured->have_post