• Dear sir’s, I’m developing plugin with multiple custom post types where relationships between them are done through storing id’s of the other as meta fields;

    event custom post type
    instructor custom post type
    attendee custom post type

    attendee stores the meta ‘event-id’, while event stores the meta ‘instructor-id’

    What I’m trying to express a join query that will filter the admin table for attendee based on instructor-id.

    I’m triggering my ‘pre_get_posts’ filter function but I don’t know how to best build my $query->get(‘meta_query’) when $_GET[‘instructor-id’] is set.

    Any help to get on how to set my ‘meta_query’ so I can filter based on instructor?

Viewing 1 replies (of 1 total)
  • Moderator threadi

    (@threadi)

    This sould match your question:

    function custom_post_filter( WP_Query $query ): void {
        if ( is_admin() && $query->is_main_query() && !empty($_GET['instructor-id']) ) {
            $query->set(
                'meta_query',
                array(
                    array(
                        'key' => 'instructor-id',
                        'value' => absint($_GET['instructor-id']),
                        'compare' => '='
                    ),
                )
            );
        }
    }
    add_action( 'pre_get_posts', 'custom_post_filter' );
Viewing 1 replies (of 1 total)

The topic ‘Query related custom post type’ is closed to new replies.