- This topic has 4 replies, 2 voices, and was last updated 3 years, 3 months ago by
Fernando.
-
AuthorPosts
-
December 29, 2022 at 9:42 pm #2477542
John
Hi team,
I’m sorry I keep posting, I feel like my inquiries are very similar and yet from the various examples I get, I’m not able to rebuild a different filter because there is always something a bit different. I wish you had some sort of query loop filter code generator 🙂
I’m hoping this one is easy (for you): On an
eventpost type, I have a custom field that outputs the post ID of another post type (provider). On that Provider page, I would like to build a query loop that is filtered by this field, so that only theeventpages for which the custom field return the same ID as the current page are displayed.The logic is as follows:
1. Eacheventpage has anevent_hostcustom field that outputs the post ID of theproviderit is attached to.
2. On eachproviderpage, I want to display a query loop of Events attached to that Provider (i.e. onlyeventpages whereevent_hostequals the current Provider page ID are displayed).Hopefully that makes sense. How can I compare the value of the
event_hostcustom field to the current page ID and return only the posts that match?Thank you
December 29, 2022 at 9:56 pm #2477554Fernando Customer Support
Hi John,
Theoretically, the code should look like this:
add_filter( 'generateblocks_query_loop_args', function( $query_args, $attributes ) { if ( ! is_admin() && ! empty( $attributes['className'] ) && strpos( $attributes['className'], 'cu-query-loop' ) !== false ) { return array_merge( $query_args, array( 'meta_query' => array( array( 'key' => 'ADD-PROVIDER-META-SLUG-HERE', 'value' => get_the_ID(), 'compare' => '=', ) ) ) ); } return $query_args; }, 10, 2 );Add
cu-query-loopto the class list of the Grid Block of the Query Loop.Replace
ADD-PROVIDER-META-SLUG-HEREwith the corresponding post meta field name.Adding PHP: https://docs.generatepress.com/article/adding-php/#code-snippets
December 29, 2022 at 9:59 pm #2477557John
Thanks Fernando, I will try that as soon as my little one is in bed. In the meantime, you’ve been helping me so much I feel like I owe you a beer at the very elast. If you accept tips, give me your paypal/cashapp/crypto wallet address and I’ll send you one 😉
(not joking)December 30, 2022 at 2:49 pm #2478400John
It works! Thank you very much 🙂
January 1, 2023 at 5:07 pm #2480066Fernando Customer Support
You’re welcome, John! We accept tips here: https://generatepress.com/ongoing-development/
Thank you and Happy New Year! 🙂
-
AuthorPosts
- You must be logged in to reply to this topic.