Skip Navigation

[Resolved] Re-open ticket please? Assign to Waqar

This support ticket is created 5 years, 12 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Karachi (GMT+05:00)

Tagged: 

This topic contains 2 replies, has 2 voices.

Last updated by julieP 5 years, 11 months ago.

Assisted by: Waqar.

Author
Posts
#1156272

Hi Waqar

You kindly helped me out with producing a custom shortcode here:-
https://toolset.com/forums/topic/use-views-output-as-arguments-in-wpv-conditional-shortcode/#post-1118287

I expanded on that code slightly by adding in an additional argument against post type a. This is the code I'm using (the additional lines I added are on lines 7, 16 & 17):-

add_shortcode('check_count_condition', 'check_A_B_condition_func');
function check_A_B_condition_func() {
 
    $slug_post_type_a = "post_a";
    $slug_post_type_b = "post_b";
    $custom_field_days_a = "wpcf-total-days";
    $custom_field_check_a = "wpcf-check";
 
     
    // get total number of days from all post types A
    
   $args_a = array(
    'posts_per_page'   => -1,
    'post_type'        => $slug_post_type_a,
    'post_status'      => 'publish',
    'meta_key'         => $custom_field_check_a,
    'meta_value'       => 'Open',
   );
 
   $posts_a_array = get_posts( $args_a );
 
   $posts_a_days_count = 0;
 
   foreach ( $posts_a_array as $post_a ) {
     $posts_a_days_count = $posts_a_days_count + get_post_meta( $post_a -> ID, $custom_field_days_a, true );
   }
 
    // get total number of post types B
 
   $args_b = array(
    'posts_per_page'   => -1,
    'post_type'        => $slug_post_type_b,
    'post_status'      => 'publish',
   );
 
   $posts_b_array = get_posts( $args_b );
 
   $posts_b_count = count($posts_b_array);
 
   // return "1" if total number of days from all post types A is greater than total number of post type B or else "0"
   if ($posts_a_days_count > $posts_b_count)
   {
    return 1;
   }
   else
   {
    return 0;
   }
}

I'd like the check to be made for posts where the custom field value of 'wpcf-start-date' in post a is less than or equal to TODAY. I already have meta_key and meta_value in the args for post a to check that custom field 'wpcf-check' equals 'Open'; I'm unclear about how I expand it to two custom fields. Can you help please? Many thanks

#1157588

Hi Julie,

Thanks for asking! I'd be happy to help.

To combine two custom fields conditions in your query, you can update it as follows:


add_shortcode('check_count_condition', 'check_A_B_condition_func');
function check_A_B_condition_func() {
  
  $slug_post_type_a = "post_a";
  $slug_post_type_b = "post_b";
  $custom_field_days_a = "wpcf-total-days";
  $custom_field_check_a = "wpcf-check";
  
  // get today's date
  $todays_date = date("Y-m-d");
  
  // convert today's date to Unix timestamp
  $today = strtotime($todays_date);
      
  // get total number of days from all post types A, where "wpcf-check" field value is "Open" and "wpcf-start-date" is less than or equal to "Today".
     
   $args_a = array(
    'posts_per_page'   => -1,
    'post_type'        => $slug_post_type_a,
    'post_status'      => 'publish',
    'meta_query' => array(
      'relation' => 'AND',
        array(
          'key'     => $custom_field_check_a,
          'value'   => 'Open',
          ),
        array(
          'key'     => 'wpcf-start-date',
          'value'   => $today,
          'type'    => 'NUMERIC',
          'compare' => '<=',
          ),
    ),
   );
  
   $posts_a_array = get_posts( $args_a );
  
   $posts_a_days_count = 0;
  
   foreach ( $posts_a_array as $post_a ) {
     $posts_a_days_count = $posts_a_days_count + get_post_meta( $post_a -> ID, $custom_field_days_a, true );
   }
  
    // get total number of post types B
  
   $args_b = array(
    'posts_per_page'   => -1,
    'post_type'        => $slug_post_type_b,
    'post_status'      => 'publish',
   );
  
   $posts_b_array = get_posts( $args_b );
  
   $posts_b_count = count($posts_b_array);
  
   // return "1" if total number of days from all post types A is greater than total number of post type B or else "0"
   if ($posts_a_days_count > $posts_b_count)
   {
    return 1;
   }
   else
   {
    return 0;
   }
}

Note: More examples for using custom field queries are available at:
https://codex.wordpress.org/Class_Reference/WP_Query#Custom_Field_Parameters

For a more personalized assistance around custom programming, you can also hire someone from our list of recommended contractors:
https://toolset.com/contractors/

I hope this helps!

regards,
Waqar

#1158525

Excellent Waqar, thank you! I appreciate the Codex link - don't know why I haven't been able to find that.....