Sauter la 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 5 années. 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
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é : 

Ce sujet contient 30 réponses, a 3 voix.

Dernière mise à jour par geoffD Il y a 5 années.

Assisté par: Christian Cox.

Auteur
Publications
#1419491

My issue is resolved now. Thank you!