Skip Navigation

[Gelöst] Query Filter in Views for Blank field

Dieser Thread wurde gelöst. Hier ist eine Beschreibung des Problems und der Lösung.

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 vor 4 Jahren, 12 Monaten. 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.

Heute stehen keine Supporter zur Arbeit im Werkzeugsatz-Forum zur Verfügung. Sie können gern Tickets erstellen, die wir bearbeiten werden, sobald wir online sind. Vielen Dank für Ihr Verständnis.

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)

Dieses Thema enthält 30 Antworten, hat 3 Stimmen.

Zuletzt aktualisiert von geoffD vor 4 Jahren, 11 Monaten.

Assistiert von: Christian Cox.

Author
Artikel
#1419491

My issue is resolved now. Thank you!