Filter Relationships Query
Support › MB Relationships › Filter Relationships Query
- This topic has 4 replies, 2 voices, and was last updated 3 years, 5 months ago by
JC.
-
AuthorPosts
-
October 27, 2021 at 7:36 PM #31549
JC
ParticipantHello,
I'm working on an events website. I have custom post types for venues and dates. The relationship is from the dates to the venues. I'm working on a query to show all dates from a venue single view. This one is working:
$connected = new WP_Query( [ 'relationship' => [ 'id' => 'fromdatestovenues', 'to' => get_the_ID(), ], 'nopaging' => true, ] );
How can I get only dates from the relationship where a custom select field ('wochentag_select') is set to a specific value?
Best
Jan
October 27, 2021 at 11:13 PM #31555Long Nguyen
ModeratorHi,
You can add the meta query (field ID and value) to the arguments of the query to filter dates, please read more on the WP documentation https://developer.wordpress.org/reference/classes/wp_query/#custom-field-post-meta-parameters
October 27, 2021 at 11:36 PM #31556JC
ParticipantHi Long,
thanks for your answer. I'm probably doing something wrong, because the result isn't filtered:
$connected = new WP_Query( [ 'relationship' => [ 'id' => 'fromdatestovenues', 'to' => get_the_ID(), 'meta_key' => 'wochentag_select', 'meta_value' => $dertag,], 'nopaging' => true, ] );
doesn't show any posts:
$connected = new WP_Query( [ 'relationship' => [ 'id' => 'fromdatestovenues', 'to' => get_the_ID(), ], 'meta_key' => 'wochentag_select', 'meta_value' => $dertag, 'nopaging' => true, ] );
Is it relevant that the custom field is not in the post I'm running the query from, but in the post with the relationship?
In Views (Singleview) the working query is:
{% for post in relationships.fromdatestovenues.from|filter(post => post.wochentag_select.name == dertag or post.wochentag_select.name == heute)|sort((a, b) => b.wochentag_select <=> a.wochentag_select ?: a.startzeit <=> b.startzeit) %}
But I have to rewrite it to php.
October 28, 2021 at 10:25 AM #31566Long Nguyen
ModeratorHi,
What is the value that you assign to the variable
$dertag
? Please assign a specific value to the argumentmeta_value
to check if it works.
I also see that in the View code, the query usesfrom
instead ofto
in the relationship arguments, you can change tofrom
and re-check it.On another note, adding the argument
post_type
to the query might help to specify a post type.$connected = new WP_Query( [ 'post_type' => 'dates', //here 'relationship' => [ 'id' => 'fromdatestovenues', 'to' => get_the_ID(), // change to 'from'? ], 'meta_key' => 'wochentag_select', 'meta_value' => 'abcd', 'nopaging' => true, ] );
October 28, 2021 at 6:17 PM #31592JC
ParticipantHi,
thanks for your answer. I tried your suggestions in every combination without any success. I guess there is another problem. I'll try to solve this with custom categories instead of relationships then.
Best
Jan
-
AuthorPosts
- You must be logged in to reply to this topic.