我正在尝试创建一个简单的Ajax帖子过滤器,它可以处理日期和评论,但不能处理顶部视图或顶部喜欢的内容。
那么,我怎样才能isset( $_POST[\'top-views\']
和isset( $_POST[\'top-likes\']
选择时工作。
非常感谢你的帮助。
类型
<select name="misha_order_by" id="misha_order_by">
<option name="top-views" value="top-views">Top Views</option>
<option name="top-likes" value="top-likes">Top Likes</option>
<option value="comment_count-DESC">Comments ↓</option>
<option value="comment_count-ASC">Comments ↑</option>
<option value="date-DESC">Date ↓</option>
<option value="date-ASC">Date ↑</option>
</select>
Ajax函数。
add_action(\'wp_ajax_mishainsightsfilter\', \'misha_insights_filter_function\');
add_action(\'wp_ajax_nopriv_mishainsightsfilter\', \'misha_insights_filter_function\');
function misha_insights_filter_function(){
global $current_user;
get_currentuserinfo();
// example: date-ASC
$order = explode( \'-\', $_POST[\'misha_order_by\'] );
$params = array(
\'posts_per_page\' => $_POST[\'misha_number_of_results\'], // when set to -1, it shows all posts
\'author\' => $current_user->ID,
\'orderby\' => $order[0], // example: date
\'order\' => $order[1] // example: ASC
);
if( isset( $_POST[\'top-views\'] ) )
$params = array(
\'author\' => $current_user->ID,
\'meta_key\' => \'post_views_count\',
\'orderby\' => \'meta_value_num\',
\'order\' => \'DESC\'
);
if( isset( $_POST[\'top-likes\'] ) )
$params = array(
\'author\' => $current_user->ID,
\'meta_key\' => \'_post_like_count\',
\'orderby\' => \'meta_value_num\',
\'order\' => \'DESC\'
);
query_posts( $params );
global $wp_query;
if( have_posts() ) :
ob_start(); // start buffering because we do not need to print the posts now
while( have_posts() ): the_post();
the_title();
endwhile;
$posts_html = ob_get_contents(); // we pass the posts to variable
ob_end_clean(); // clear the buffer
else:
$posts_html = \'<p>Nothing found for your criteria.</p>\';
endif;
// no wp_reset_query() required
echo json_encode( array(
\'posts\' => json_encode( $wp_query->query_vars ),
\'max_page\' => $wp_query->max_num_pages,
\'found_posts\' => $wp_query->found_posts,
\'content\' => $posts_html
) );
die();
}
最合适的回答,由SO网友:Sally CJ 整理而成
我还没有测试代码,但这应该会有所帮助
option
元素没有name
属性:
<option name="top-views" value="top-views">Top Views</option>
<option name="top-likes" value="top-likes">Top Likes</option>
因此
$_POST[\'top-views\']
例如,不会设置,或该值不是
top-views
在上面
option
.
使用适当的option
标记基于以下标记:
<option value="comment_count-DESC">Comments ↓</option>
您的自定义
option
应为:
<option value="{key}-{order}">{label}</option>
So(按降序排序):
<option value="top_views-DESC">Top Views</option>
<option value="top_likes-DESC">Top Likes</option>
请注意
{key}
不应该有破折号(
-
).
正在分析排序{key}
和{order}
在
misha_insights_filter_function()
功能,排序
{key}
(即
orderby
参数)和
{order}
分析如下:
$order = explode( \'-\', $_POST[\'misha_order_by\'] );
例如,如果选择了“俯视图”,则
option
值为
top_views-DESC
, 然后
$order[0]
可能是
top_views
和
$order[1]
可能是
DESC
.
然后您可以自定义$param
像这样:
if ( \'top_views\' === $order[0] ) {
$params[\'meta_key\'] = \'post_views_count\';
$params[\'orderby\'] = \'meta_value_num\';
}
类似地,如果选择了“Top Likes”,并且
option
值为
top_likes-DESC
, 您可以复制粘贴以上内容
if
阻止并替换相关值:
if ( \'top_likes\' === $order[0] ) {
$params[\'meta_key\'] = \'_post_like_count\';
$params[\'orderby\'] = \'meta_value_num\';
}