我收到帖子并按标签过滤Ayah。我得到了所有的帖子。这很有效。但是,当我到达帖子的父页面时,我需要获取唯一的子帖子并过滤它们。我在请求中使用“post\\u parent”=>$post->ID,但这在函数中不起作用。如果我在我想要获取它的页面上使用查询,它会很好地打印出来。
/*Ajax Filter*/
//Get Applications Filters
function get_applications_filters()
{
$args = array( \'taxonomy\' => array( \'products_tags\'));
$terms = get_terms( $args, $deprecated );
$filters_html = false;
if( $terms ):
$filters_html = \'<ul>\';
foreach( $terms as $term )
{
$term_id = $term->term_id;
$term_name = $term->name;
$filters_html .= \'<li class="term_id_\'.$term_id.\'">\'.$term_name.\'<input type="checkbox" name="filter_applications[]" value="\'.$term_id.\'"></li>\';
}
$filters_html .= \'<li class="clear-all">Reset Filter</li>\';
$filters_html .= \'</ul>\';
return $filters_html;
endif;
}
//Add Ajax Actions
add_action(\'wp_ajax_applications_filter\', \'ajax_applications_filter\');
add_action(\'wp_ajax_nopriv_applications_filter\', \'ajax_applications_filter\');
//Construct Loop & Results
function ajax_applications_filter()
{
$query_data = $_GET;
$applications_terms = ($query_data[\'applications\']) ? explode(\',\',$query_data[\'applications\']) : false;
$tax_query = ($applications_terms) ? array( array(
\'taxonomy\' => \'products_tags\',
\'field\' => \'id\',
\'terms\' => $applications_terms
) ) : false;
$search_value = ($query_data[\'search\']) ? $query_data[\'search\'] : false;
//$paged = (isset($query_data[\'paged\']) ) ? intval($query_data[\'paged\']) : 1;
$products_args = array(
\'post_type\' => \'products\',
\'s\' => $search_value,
\'posts_per_page\' => -1,
\'tax_query\' => $tax_query,
\'post_parent\' => $post->ID,
//\'paged\' => $paged
);
$products_loop = new WP_Query($products_args);
if( $products_loop->have_posts() ):
while( $products_loop->have_posts() ): $products_loop->the_post();
get_template_part(\'template-parts/content-products-archive\');
endwhile;
else:
get_template_part(\'content-none\');
endif;
wp_reset_query();
die();
}
阿贾克斯
//Applications Ajax Filtering
jQuery(function($) {
//Load posts on page load
applications_get_posts();
//If list item is clicked, trigger input change and add css class
$(\'#applications-filter li\').live(\'click\', function(){
var input = $(this).find(\'input\');
if ( $(this).attr(\'class\') == \'clear-all\' )
{
$(\'#applications-filter li\').removeClass(\'selected\').find(\'input\').prop(\'checked\',false);
applications_get_posts();
}
else if (input.is(\':checked\'))
{
input.prop(\'checked\', false);
$(this).removeClass(\'selected\');
} else {
input.prop(\'checked\', true);
$(this).addClass(\'selected\');
}
input.trigger("change");
});
//If input is changed, load posts
$(\'#applications-filter input\').live(\'change\', function(){
applications_get_posts(); //Load Posts
});
//Find Selected Applications
function getSelectedApplications()
{
var applications = [];
$("#applications-filter li input:checked").each(function() {
var val = $(this).val();
applications.push(val);
});
return applications;
}
//Fire ajax request when typing in search
$(\'#applications-search input.text-search\').live(\'keyup\', function(e){
if( e.keyCode == 27 )
{
$(this).val(\'\');
}
applications_get_posts(); //Load Posts
});
$(\'#submit-search\').live(\'click\', function(e){
e.preventDefault();
applications_get_posts(); //Load Posts
});
//Get Search Form Values
function getSearchValue()
{
var searchValue = $(\'#applications-search input.text-search\').val();
return searchValue;
}
//If pagination is clicked, load correct posts
$(\'.applications-filter-navigation a\').live(\'click\', function(e){
e.preventDefault();
var url = $(this).attr(\'href\');
var paged = url.split(\'&paged=\');
applications_get_posts(paged[1]); //Load Posts (feed in paged value)
});
//Main ajax function
function applications_get_posts(paged)
{
var paged_value = paged;
var ajax_url = ajax_applications_params.ajax_url;
$.ajax({
type: \'GET\',
url: ajax_url,
data: {
action: \'applications_filter\',
applications: getSelectedApplications,
search: getSearchValue(),
paged: paged_value
},
beforeSend: function ()
{
},
success: function(data)
{
//Hide loader here
$(\'#applications-results\').html(data);
$(\'#applications-results .wrap\').matchHeight({
byRow: true
});
},
error: function()
{
$("#applications-results").html(\'<p>There has been an error</p>\');
}
});
}
});
最合适的回答,由SO网友:Chandra Kumar 整理而成
要检索帖子详细信息,您必须自己发送数据。
在您的JavaScript 您没有发送的代码post id.
按如下方式更新代码:
data:{
action: "applications_filter",
post_id: current_post_id, //current_post_id should either parsed from DOM or you can write your ajax in PHP file
}
您必须发送
post_id 在php文件中使用之前。
然后您可以通过GET检索
function ajax_applications_filter(){
$post_id = $_GET[\'post_id\'];
//Now set post_parent, it will be work
$products_args = array(
\'post_type\' => \'products\',
\'s\' => $search_value,
\'posts_per_page\' => -1,
\'tax_query\' => $tax_query,
\'post_parent\' => $post_id
//\'paged\' => $paged
);
}