- This topic has 16 replies, 2 voices, and was last updated 2 years, 2 months ago by .
-
Topic
-
I have a listing of people that I am showing on the post type archive. For taxonomies I can use a simple query var with the taxonomy term to filter out the listing of all posts into the posts only inside that taxonomy. I would like to do the same thing with relationships.
Is there a built-in query var for relationships? I think that is
relationship
. But then how would I use it in the URL bar? Pass a single ID?for example –
domain.com/people/?relationship=320
What I was thinking about doing is add a custom query arg then pass a single post ID then use that post ID inside a
pre_get_post
filter to only show the posts related to that ID. Does that sound like a good plan?function custom_query_vars_filter($vars) { $vars[] .= 'relationships'; return $vars; } add_filter( 'query_vars', 'custom_query_vars_filter' );
function prefix_get_related_posts( $query ) { $post_id = get_query_var('relationships'); if ( empty( $post_id ) || is_admin() ) { return $query; } if( is_post_type_archive( 'poa_person' ) && $query->is_main_query() ) { $relationship = array( 'id' => 'poa_practice_to_poa_person', 'from' => $post_id, ); $query->set( 'relationship', $relationship ); } return $query; } add_filter( 'pre_get_posts', 'prefix_get_related_posts', 10, 1);
This does not work just yet. Can you help shed some light on this query?
- You must be logged in to reply to this topic.