I am using repeatable meta boxes what i want to achieve is display number of post which have particular meta key such as
Country Pakistan
City Islamabad
Age 12
Age 12
Meta key "Age" is used in 2 posts.
No i want structure like Age in Post 2 , Islamabad in Post 1 , Pakistan in Post 1.
/**
* Display callback for the submenu page.
*/
function iif_register_submenu_form_details_callback() {
$id = get_current_user_id();
$loop = new WP_Query( array(
'post_type' => 'iff-info-forms',
'post_status' => 'publish',
'author' => $id,
'meta_query' => array(
array(
'key' => 'repeatable_fields',
//'value' => $user_data_new,
'compare' => 'LIKE'
))
) );
//print_r($loop);
while ( $loop->have_posts() ) : $loop->the_post();
$info = get_post_meta(get_the_id(), 'repeatable_fields', true);
if ( $info ) {
// echo '<strong>Emergency Contacts:</strong><br />';
foreach ( $info as $emergency_contact_metas ) {
echo '<strong>' . esc_html( $emergency_contact_metas['iif_label'] ) .'</strong> ' . esc_html( $emergency_contact_metas['iif_details'] ) . '<br />';
}
}
endwhile;
}
OUTPUT SHOWING IS Below
Country Pakistan
City Islamabad
Age 12
Age 12
Related
I like to display unit prices at variable products. I like it to do based on a product attribute whic contains the following: 5 kg, 2 l etc. So, I get the attribute, extract the numbers from it and do some math. The problem is, that all of the code displays the multipier number of 1. Here is my code:
function product_variation_unit_price( $data, $product, $variation ) {
$price = wc_get_price_to_display( $variation );
$unit = array_shift( wc_get_product_terms( $product->id, 'pa_unit', array( 'fields' => 'names' ) ) );
$unit_number_extracted = preg_replace('/[^0-9]/', '', $unit);
if ( (!empty($unit) ) AND $unit_number_extracted == 1 ) {
$unit_price = sprintf( __( 'Gross unit price: %s', 'loremipsum' ), wc_price($price * $unit_number_extracted) );
} else if( (!empty($unit) ) AND $unit_number_extracted > 1 ) {
$unit_price = sprintf( __( 'Gross unit price: %s', 'loremipsum' ), wc_price($price / $unit_number_extracted) );
}
$data['price_html'] .= '<span> ' . $unit_price . '</span>';
return $data;
}
add_filter( 'woocommerce_available_variation', 'product_variation_unit_price', 10, 3 );
I guess I should foreach the variations, but I don't know how to do that in this situation.
Thanks if you can help!
The code below is said to give the total spent spent over 30 days (if I'm not mistaken). But in my test, it calculates for the current month. How can I not update this code for last 6 months(180 days). The important thing is to be able to easily change the coverage day and make the calculation for the completed orders. Thanks.
Code resource:https://github.com/anaymark/woocommerce-monthly-spent-by-user/blob/master/total-spent-month-by-user.php
function total_spent_for_user_30days( $user_id=null ) {
if ( empty($user_id) ){
$user_id = get_current_user_id();
}
$today_year = date( 'Y' );
$today_month = date( 'm' );
$day = date( 'd' );
if ($today_month == '01') {
$month = '12';
$year = $today_year - 1;
} else{
$month = $today_month - 1;
$month = sprintf("%02d", $month);
$year = $today_year - 1;
}
// ORDERS FOR LAST 30 DAYS (Time calculations)
$now = strtotime('now');
$gap_days = 30;
$gap_days_in_seconds = 60*60*24*$gap_days;
$gap_time = $now - $gap_days_in_seconds;
$args = array(
'post_type' => 'shop_order',
'post_status' => array( 'wc-completed' ),
// all posts
'numberposts' => -1,
// for current user id
'meta_key' => '_customer_user',
'meta_value' => $user_id,
'date_query' => array(
//orders published on last 30 days
'relation' => 'OR',
array(
'year' => $today_year,
'month' => $today_month,
),
array(
'year' => $year,
'month' => $month,
),
),
);
// GET ALL ORDERS
$customer_orders = get_posts( $args );
$count = 0;
$total = 0;
$no_orders_message = __('No orders this month.', 'mytheme');
if (!empty($customer_orders)) {
$customer_orders_date = array();
foreach ( $customer_orders as $customer_order ){
$customer_order_date = strtotime($customer_order->post_date);
// PAST 30 DAYS
if ( $customer_order_date > $gap_time ) {
$customer_order_date;
$order = new WC_Order( $customer_order->ID );
$order_items = $order->get_items();
$total += $order->get_total();
// Going through each current customer items in the order
foreach ( $order_items as $order_item ){
$count++;
}
}
}
$monthly_spent_by_user = floatval( preg_replace( '#[^\d.]#', '', $total, $count ) );
return $monthly_spent_by_user;
} else {
return $no_orders_message;
}
}
add_shortcode( 'spent-last-month', 'total_spent_for_user_30days' );
I'm building some stats for a directory website and I got stuck calculating the average age of listings assigned to a particular listing type.
I figured out calculating listing age in days by ID:
$today = date( 'Y-m-d' );
$listing_date = get_the_date( 'Y-m-d', $id );
$diff = strtotime( $today ) - strtotime( $listing_date );
$age = round( $diff / 86400 );
I figured out how to pull listings assigned to a particular listing type by term ID:
$args = array(
'posts_per_page' => -1,
'fields' => 'ids',
'post_type' => 'listing',
'post_status' => 'publish',
'tax_query' => array(
array(
'taxonomy' => 'listing-type',
'field' => 'id',
'terms' => $id
)
)
);
$ids = get_posts( $args );
This gives me an array of IDs of all listings assigned to a particular listing type.
Now I need to calculate the total age in days for all those listings I retrieved IDs for.
I figured $cnt = count( $ids ); to get the total number of retrieved listings (IDs), but I can't figure out calculating the total age in days for those.
With a foreach loop :
<?php
// Your function to get the diff
function get_diff($id) {
$today = date( 'Y-m-d' );
$listing_date = get_the_date( 'Y-m-d', $id );
$diff = strtotime( $today ) - strtotime( $listing_date );
$age = round( $diff / 86400 );
return $age;
}
// We want to store all the diff
$diffs = [];
foreach($ids as $id) {
$diffs[] = get_diff($id);
}
// Get the average | We filter to remove empty values
$diffs = array_filter($diffs);
if(count($diffs)) {
echo $average = array_sum($diffs)/count($diffs);
}
I would like to group the results of my query if the meta_value is the same for any results. I would expect an output like this:
Group 1
- 1111
- 1111
- 1111
Group 2
- 1234
- 1234
Here is my query:
$args = array(
'post_type' => 'product-api',
'orderby' => 'title',
'order' => 'ASC',
'posts_per_page' => -1,
'meta_query' => array(
array(
'key' => 'last_updated',
'value' => '',
'compare' => '!=',
)
),
);
$the_query = new WP_Query( $args );
if ( $the_query->have_posts() ) {
while ( $the_query->have_posts() ) {
$count++;
$the_query->the_post();
$model = get_post_meta(get_the_id(), 'product_model', true);
echo $model;
}
}
wp_reset_postdata();
What would be the best approach to achieve this?
In wordpress category.php I have this code:
if ( is_category('cat-1') ) {
$the_query = new WP_Query( array ( 'post_type' => 'mueble', 'category_name' => 'cat-1' , 'posts_per_page' => 3 ) ) ;
}
if ( is_category('cat-2') ) {
$the_query = new WP_Query( array ( 'post_type' => 'mueble', 'category_name' => 'cat-2' , 'posts_per_page' => 3 ) ) ;
}
if ( is_category('cat-3') ) {
$the_query = new WP_Query( array ( 'post_type' => 'mueble', 'category_name' => 'cat-3' , 'posts_per_page' => 3 ) ) ;
}
while ( $the_query->have_posts() ) : $the_query->the_post();
...
endwhile; wp_reset_postdata();
How can I simplify the code? I have 30 categories and too many conditionals isn't ideal...
if you know the exact number of categories, you can try a for loop:
for ($x=0; $x<=30; $x++) {
if ( is_category('cat-' . $x) ) { $the_query = new WP_Query( array ( 'post_type' => 'mueble', 'category_name' => 'cat-' . $x , 'posts_per_page' => 3 ) ) ; }
}