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.
Hoy no hay técnicos de soporte disponibles en el foro Juego de herramientas. Siéntase libre de enviar sus tiques y les daremos trámite tan pronto como estemos disponibles en línea. Gracias por su comprensión.
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)
Este tema contiene 30 respuestas, tiene 3 mensajes.
Última actualización por hace 4 años, 11 meses.
Asistido por: Christian Cox.