I have post types Boat and Drill. I've got a one-many relationship set for them. I have a view set up to list out all drills. I want to filter the list of drills to only be the drills related to a specific boatid.
Here's my current code- how can I add the boat-drill relationship filter to the query?
$query_args['meta_query'][] = array(
// How do I add the boat-drill relationship filter here?
);
}
return $query_args;
}
And yes, I know I could do it by creating a filter but having a shortcode parameter (eg. [wpv-view name="view-name" wpvrelatedto="123"]) breaks the view in this case. (I'm using it to populate a generic select field- hence the complexity.)
Before going down the code route, from your description it sounds like you want the View to return posts which are related to a known, specific post ($boatid = 1234), rather than, say, the boat where the View is displayed.
You can do that in the View settings.
See in my screenshot where I'm adding a Query Filter for a relationship, and I am specifying a specific post that is the starting point for find related posts. (In my example the relationship is between left and right posts, and I'm finding right posts related to the single post "left hand".)
OK, you must be getting that $boatid from somewhere and I would have thought you could then pass it as a shortcode attribute for example and still handle it within Views without recourse to the API, but you know your scenario better than I.
Views queries are built on top of standard WP Queries.
The way relationship filters work is to run a separate relationship query to get all of the related posts according to the filter settings and then add the resulting array of post ID's as a post__in argument to the main query, so you would need to do the same.
Edit your View and delete any relationship Query Filter you added in the GUI.
Here is a very generic example of the kind of thing you will need to implement: