I am using the following function/shortcode in order to output an average global rating using ACF :
function get_average_rating($post_id) {
$rating_sum = 0;
$reviews_of_post = get_posts( array(
'post_type' => 'avis',
'posts_per_page' => -1,
'meta_query' => array(
array(
'key' => 'produit',
'value' => $post_id,
'compare' => '=',
),
),
) );
if ( empty( $reviews_of_post ) ) {
return 0;
}
foreach ( $reviews_of_post as $review ) {
$rating_sum += get_field( 'note_client', 'post_' . $review->ID);
}
return number_format((float)($rating_sum / count( $reviews_of_post )),1, ',', '');
}
add_shortcode( 'note-clients', 'get_average_rating');
It always return 0 except when I manually input the post ID like :
'meta_query' => array(
array(
'key' => 'produit',
'value' => 1234,
'compare' => '=',
),
),
How can I fix this ?
Thanks a lot !
Declare global $post; before your function.
Wordpress uses $post for many of its functions within the loop.
To avoid any conflicts later you should consider use of wp_reset_query
1 . Change key 'key' => 'produit' to 'key' => 'product',
2 .
foreach ( $reviews_of_post as $review ) {
$rating_sum += get_field( 'note_client', 'post_' . $review->ID);
}
**to**
foreach ( $reviews_of_post as $review ) {
$post_id = 'post_' . $review->ID ;
$rating_sum += get_field( 'note_client', $post_id );
}
Confirm ACF key (note_client) is correct
3 . Change this
add_shortcode( 'note-clients', 'get_average_rating');
to
add_shortcode( 'average_rating', 'get_average_rating');
Please try with your post static id:
$post_id = 9; //add here your static post id
$reviews_of_post = get_posts( array(
'post_type' => 'avis',
'posts_per_page' => -1,
'meta_query' => array(
array(
'key' => 'produit',
'value' => $post_id,
'compare' => '=',
),
),
) );
Related
I am trying to make custom validation to cf7 form and then store data in a custom post type if everything is valid. My problem is that even when form inputs are not valid form still sends email and store its data. Can you help me with that. Below is my code. What should I use to validate inputs first and only then store data.
Store posted data:
function save_posted_data_ios( $posted_data ) {
if( isset($posted_data['menu-377']) == "IOS" ){
$args2 = array(
'post_type' => 'np_coupon_code',
'post_status' => 'publish',
'posts_per_page' => 1,
'meta_query' => array(
array(
'key' => 'np_ccode__email',
'value' => 'null',
'compare' => 'NOT EXISTS'
),
array(
'key' => 'np_ccode__type',
'value' => 'IOS'
)
)
);
$posts_query2 = new WP_Query($args2);
$the_count_ios = $posts_query2->found_posts;
$query = new WP_Query($args2);
while ( $query->have_posts() ) : $query->the_post();
$id = get_the_ID();
$code = get_the_title();
endwhile;
if( isset($posted_data['your-email']) ){
update_post_meta($id, 'np_ccode__email', $posted_data['your-email']);
}
$to = $posted_data['your-email'];
$subject = 'Subject';
$body = $code;
$headers = array('Content-Type: text/html; charset=UTF-8');
wp_mail( $to, $subject, $body, $headers );
return $posted_data;
wp_reset_query();
}
}
add_filter( 'wpcf7_posted_data', 'save_posted_data_ios' );```
Custom error:
function my_wpcf7_validate_ios( $result_ios, $tag ) {
$type = $tag['type'];
$name = $tag['menu-377'];
$args2 = array(
'post_type' => 'np_coupon_code',
'post_status' => 'publish',
'posts_per_page' => 1,
'meta_query' => array(
array(
'key' => 'np_ccode__email',
'value' => 'null',
'compare' => 'NOT EXISTS'
),
array(
'key' => 'np_ccode__type',
'value' => 'IOS'
)
)
);
$posts_query2 = new WP_Query($args2);
$the_count_ios = $posts_query2->found_posts;
if ( $the_count_ios == 0 && $_POST['menu-377'] == "IOS"){
$result_ios->invalidate( $tag, wpcf7_get_message( 'invalid_ios' ) );
}
return $result_ios;
}
add_filter( 'wpcf7_validate_select*', 'my_wpcf7_validate_ios' , 10, 2 );
add_filter( 'wpcf7_messages', 'mywpcf7_ios_messages' );
function mywpcf7_ios_messages( $messages ) {
return array_merge( $messages, array(
'invalid_ios' => array(
'description' => __( "No IOS Coupons Left", 'contact-form-7' ),
'default' => __( 'No IOS Coupons Left.', 'contact-form-7' )
)
));
}
i want to make this thing - get a list of options of some attribute of products in category with known id. So, the category id is $catid, the attribute is $attr.
So, to my mind, i have to do these steps:
0) get all the products ids from category with id
get all attributes of products with these ids
if product's attribute is $somename, get it's option, store it in array $options
Finally there must be an array like [s,m,l,xl,xxl] for example, if the searching attribute is "size" and the category is "t-shirts".
So what is done:
function get_options_of_attribute() {
$catid = "6776"; // T-shirts
$attr = "size";
$all_ids = get_posts( array(
'post_type' => 'product',
'numberposts' => -1,
'post_status' => 'publish',
'fields' => 'ids',
'tax_query' => array(
array(
'taxonomy' => 'product_cat',
'field' => 'term_id',
'terms' => $current_cat_id,
'operator' => 'IN'
)
),
));
$ids = '';
$values = '';
foreach ( $all_ids as $id ) {
$ids .= $id.' ';
foreach( wc_get_product_terms( $id, $attr ) as $attribute_value ){
$values .= $attribute_value.' ';
}
}
// return $ids;
return $values;
}
add_shortcode( 'listofoptions', 'get_options_of_attribute' );
Nothing happens. The part of code, which generates the $ids is working, something wrong with wc_get_product_terms() i think...
Finally i solved it with:
function get_options_of_attribute() {
$current_cat_id = "6776"; // T-shirts
$attr = "size";
$all_ids = get_posts( array(
'post_type' => 'product',
'numberposts' => -1,
'post_status' => 'publish',
'fields' => 'ids',
'tax_query' => array(
array(
'taxonomy' => 'product_cat',
'field' => 'term_id',
'terms' => $current_cat_id,
'operator' => 'IN'
)
),
));
$ids = '';
$value = array();
$values = '';
foreach ( $all_ids as $id ) {
$ids .= $id.' ';
$value[] = wc_get_product_terms($id,'pa_'.$attr, array( 'fields' => 'names'))[0];
}
$uniquevalue = array_unique($value);
foreach ( $uniquevalue as $uv ) {
$values .= $uv.' ';
}
return $values;
}
add_shortcode( 'listofoptions', 'get_options_of_attribute' );
I have an issue that is doing my head in, I'm trying to sort my posts using pre_get_posts by and ACF field that is a text field.
Here is my code:
function my_pre_get_posts( $query ) {
// do not modify queries in the admin
if( is_admin() ) {
return $query;
}
if( $query->is_main_query() && $query->is_tax('locations')) {
$query->set('meta_key', 'level');
$query->set('orderby', 'meta_value');
$query->set('order', 'DESC');
}
// return
return $query;
}
add_action('pre_get_posts', 'my_pre_get_posts');
In the ACF config, it's set to text and the values of this text can be Bronze, Silver, Gold etc.
UPDATE
I've now changed the level field to a number and switch it round 1 = Bronze, 2 = Silver etc.
Still, I get nothing.
When I run the below nothing gets returned.
Any ideas?
I think you could be inadvertently overwriting the whole query. Try this (untested) snippet:
function my_pre_get_posts( $query ) {
if( ! is_admin() && $query->is_main_query() && $query->is_tax('locations')) {
$query->set('meta_key', 'level');
$query->set('orderby', 'meta_value');
$query->set('order', 'DESC');
}
}
add_action('pre_get_posts', 'my_pre_get_posts');
Note that $query is not returned.
Good luck!
So in the end of switched it from the functions.php file to the taxonomy template.
Here is my finished code:
<?php
if(isset($_GET['type'])) {
if($_GET['type'] == 'villa') {
$filter = array(
'key' => 'type_name',
'value' => 'Villa',
'compare' => '=',
);
} elseif ($_GET['type'] == 'apartment') {
$filter = array(
'key' => 'type_name',
'value' => 'Apartment',
'compare' => '=',
);
} elseif ($_GET['type'] == 'alls') {
$filter = array(
'relation' => 'or',
array(
'key' => 'type_name',
'value' => 'Villa',
'compare' => '=',
),
array(
'key' => 'type_name',
'value' => 'Apartment',
'compare' => '=',
)
);
}
}
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array(
'post_type' => 'properties',
'post_per_page' => 12,
'paged' => $paged,
'meta_key' => 'level',
'orderby' => 'meta_value',
'order' => 'DESC',
'tax_query' => array(
array(
'taxonomy' => 'locations',
'field' => 'slug',
'terms' => $term->slug,
),
),
'meta_query' => $filter
);
// the query
$the_query = new WP_Query( $args ); ?>
Fingers crossed it helps someone else.
here is my wordpress new query loop $args
if (isset($_GET['list_shows'])) {
if ($_GET['list_shows'] == 'update') {
$orderby = 'modified';
$order = '';
} elseif ($_GET['list_shows'] == 'views') {
$orderby = 'meta_value_num';
$order = 'DESC';
} elseif ($_GET['list_shows'] == 'popularity') {
$orderby = 'comment_count';
$order = 'DESC';
}
} else {
$orderby = 'modified';
$order = '';
}
$argz=array(
'posts_per_page' => '-1',
'orderby'=>$orderby,
'order'=>$order,
'meta_query' => array(
array(
'key' => 'fragman',
'compare' => 'NOT EXISTS',
'posts_per_page' => '-1',
)
),
'date_query' => array(
array(
'column' => 'post_date_gmt',
'after' => '2 days ago',
),
'posts_per_page' => '-1',
),
);
the question is about ordering posts by meta_value_num , descending ?
my post views meta key = views, could we add this ordering parameter in this meta query ?
if it is, how can we do it. ?
Thanks.
You can pass like this
global $wp_query;
$args = array(
'meta_key' => 'views',
'orderby' => 'meta_value meta_value_num',
'order' => 'DESC'
);
query_posts( array_merge( $args , $wp_query->query ) );
I hope this will work for you.
I’m trying to query my CPT posts based on sub custom field values as in example 5 on
this tutorial.
My CPT (named “trip”) has a repeater field called “departure_date”, which has a sub field called “departure_day”:
function my_posts_where( $where ) {
$where = str_replace("meta_key = 'departure_date'", "meta_key LIKE 'departure_date'", $where);
return $where;
}
add_filter('posts_where', 'my_posts_where');
$args = array(
'numberposts' => -1,
'post_type' => 'trip',
'meta_query' => array(
array(
'key' => 'departure_day',
'value' => 0,
'compare' => '>'
)
)
);
$get_trips_date = new WP_Query( $args );
if( $get_trips_date->have_posts() ):
while ( $get_trips_date->have_posts() ) : $get_trips_date->the_post();
if( get_field('departure_date') ) {
while( has_sub_field('departure_day') ) {
echo get_sub_field('departure_day');
}
}
endwhile;
endif;
wp_reset_query();
Although sub field “departure_day” is populated for all posts, this code returns nothing. Why?
Your $args should be like this
`
$args = array(
'numberposts' => -1,
'post_type' => 'trip',
'meta_query' => array(
array(
'key' => 'departure_date_%_departure_day',
'value' => 0,
'compare' => '>'
)
)
);
`