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 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)
Ce sujet contient 30 réponses, a 3 voix.
Dernière mise à jour par Il y a 5 années.
Assisté par: Christian Cox.