Filter Posts with Elementor Query ID based on user profile information

52 Views Asked by At

I try to filter an Elementor Loop Grid with a Query ID. The outcome is supposed to be: Only Posts that have a specific taxonomy (chosen by the user) are shown in the Loop Grid. Every User has a user profile.

In this profile, the user can choose which taxonomies he is interested in. The info is saved in his profile and can be edited. On a specific other page, there is the Elementor Loop Grid. I would like to write a code that allows me to only display the posts that are assigned to the taxonomies the user has chosen in his profile.

This is the Code I have come up with so far, but it only breaks the page it is used on:

function custom_filter_themen( $query ) {
    if (is_user_logged_in()) {
        $user_themen = get_field('meine_themen', 'user_' . get_current_user_id());
        if ($user_themen) {
            $db_themen = get_terms('db_themen');
            foreach ($db_themen as $db_thema) {
                $term_slug = $db_thema->slug;
                if (!in_array($term_slug, $user_themen)) {
                    $query->set('meta_query', array(
                        array(
                            'key' => 'db_themen',
                            'value' => $term_slug,
                            'compare' => 'NOT EXISTS',
                        )
                    ));
                }
            }
        } else {
            return "Bitte wähle Themen aus.";
        }
    }
}
add_action( 'elementor/query/custom_filter_themen', 'custom_filter_themen' );

Help would be very appreciated!

With kind regards

0

There are 0 best solutions below