Skip Navigation

[Résolu] Query Filter in Views for Blank field

Ce fil est résolu. Voici une description du problème et la solution proposée.

Problem: I would like to create a custom shortcode that finds all the "person" posts where the author is the current logged-in User, and filter those posts by two custom fields. One of the fields must equal "Yes" and the other field is either blank or does not exist.

Solution: Use an associative array meta query to query by two custom fields:

/* Check if cash gift has any messages */
function cash_gift_message_func( $atts ){
   global $current_user;
   wp_get_current_user();
   $all_posts = get_posts(
    array(
      'numberposts'   => -1,
      'author' => $current_user->ID,
      'post_type'     => 'person',
      'meta_query' => array(
        'relation' => 'AND',
        'message_clause' => array(
          'relation' => 'OR',
          array(
            'key' => 'wpcf-message-for-recipient',
            'compare' => '!=',
            'value' => ''
          ),
          array(
            'key' => 'wpcf-message-for-recipient',
            'compare' => 'NOT EXISTS'
          )
        ),
        'gift_clause' => array(
            'key' => 'wpcf-cash-gift',
            'compare' => '=',
            'value' => 'Yes'
        )
      )
    )
  );
     
       $exec_number = count($all_posts);
return $exec_number;
}
     
add_shortcode( 'cash_message_func', 'cash_gift_message_func' );

Relevant Documentation:
https://developer.wordpress.org/reference/classes/wp_meta_query/

This support ticket is created Il y a 4 années et 4 mois. 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.

No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. Thank you for your understanding.

Sun Mon Tue Wed Thu Fri Sat
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

Marqué : 

This topic contains 30 réponses, has 3 voix.

Last updated by geoffD Il y a 4 années et 3 mois.

Assisted by: Christian Cox.

Auteur
Publications
#1419491

My issue is resolved now. Thank you!

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.