Well, that's not really the question to ask. Because the view's filter enforces its own way of working. For views that do not use AJAX, the view's filter will build the URL parameter so the server can know what filters are used and what values are passed for them. When the view uses AJAX, the view's search form will use POST requests to pass the filters' values to the server, so it can generate the correct results.
The question to ask is how to know what filter is being used by the view, and what value is chosen for it?
The answer is somehow complex, because of how Views work. If the view does not use AJAX, then the answer is within the URL. And we can get it from the global $_GET array. When the view uses AJAX, the answer is within the global array $_POST.
That's how the HTTP protocol and PHP work. Read more about it in these articles:
- hidden link
- hidden link
- hidden link
This being said, if your question is to calculate the average for the results of the view, based on what category is used, either for the first load or while performing AJAX calls?
Then, the shortcode needs to detect if the view is filtering using the custom taxonomy, and what taxonomy term is chosen. This can be done with the following code:
$term = "";
if ( defined('DOING_AJAX') && DOING_AJAX ) {
$search = isset( $_POST['search'] ) ? $_POST['search'] : array();
$params = isset( $search['dps_general'] ) ? $search['dps_general'] : array();
foreach( $params as $param ) {
if ( $param['name'] == 'wpv-degree' ) {
$term = $param['value'];
}
}
} else {
$term = isset( $_GET['wpv-degree'] ) ? $_GET['wpv-degree'] : "";
}
Then, we should check if it is not the default value. In your case, it is the "all" value. Check this screenshot hidden link
That can be done with the following code:
if ( strlen( $term ) > 0 && $term != '0' ){
In that case, it means, that the user is actually filtering using a taxonomy term. And we should update our WP_Query arguments:
if ( strlen( $term ) > 0 && $term != '0' ){
$args['tax_query'] = array(
array(
'taxonomy' => 'degree',
'field' => 'slug',
'terms' => $term,
),
);
}
I tested the code and it works as expected. This is the final code:
add_shortcode('rating-average', 'rating_average_func');
function rating_average_func(){
$args = array(
'post_type' => 'ddq',
'post_status' => 'publish',
// 'degree' => 'Chemistry',
);
$term = "";
if ( defined('DOING_AJAX') && DOING_AJAX ) {
$search = isset( $_POST['search'] ) ? $_POST['search'] : array();
$params = isset( $search['dps_general'] ) ? $search['dps_general'] : array();
foreach( $params as $param ) {
if ( $param['name'] == 'wpv-degree' ) {
$term = $param['value'];
}
}
} else {
$term = isset( $_GET['wpv-degree'] ) ? $_GET['wpv-degree'] : "";
}
if ( strlen( $term ) > 0 && $term != '0' ){
$args['tax_query'] = array(
array(
'taxonomy' => 'degree',
'field' => 'slug',
'terms' => $term,
),
);
}
$loop = new WP_Query( $args );
$sum = 0;
$num = 0;
//var_dump($loop);
while ( $loop->have_posts() ) : $loop->the_post();
$sum=$sum + get_post_meta(get_the_id(),'wpcf-encourage', true);
$num ++;
endwhile;
wp_reset_postdata();
if($num>0)
{
$res = $sum/$num;
}
return 'Average:'.$res;
}
Now, because you want to use AJAX for the view, you need to put the shortcode inside the view, but also outside the loop. Currently, it is outside of the view, so I drag/drop it inside the view's output, but outside the loop. Check this screenshot hidden link