Forum Replies Created

Viewing 3 replies - 1 through 3 (of 3 total)
  • Thread Starter vera1993

    (@vera1993)

    Hello everyone,

    it took me some time to work on the user profiles.

    Now, the user can choose and save the taxonomies he is interested in his profile.

    Therefore, now I try to add a filer based on this information on a specific page.

    On that page, there is an Elementor Loop Grid and I found out, that you can use a “Query ID” to filter the grids as you wish.

    Now I tried to alter the code so that only posts are shown in the loop grid that correspond with what the user has chosen in this profile.

    But the only outcome is that I break the page.

    I tried this with no influence on the page at all:

    function custom_filter_themen($query) {
    	if ($query->is_main_query() && is_user_logged_in()) { 
    		$ausgewaehlte_themen = get_user_meta( get_current_user_id(), 'meine_themen', true );
    		if ($ausgewaehlte_themen) { 
    			$tax_query = array( array( 'taxonomy' => 'db_themen', 'field' => 'slug', 'terms' => $ausgewaehlte_themen, ), ); 
    			$query->set('tax_query', $tax_query); } } } 
    add_action( 'elementor/query/custom_filter_themen', 'custom_filter_themen' );

    And this, which leads to an almost empty page (the page is empty from the point where the first loop grid is supposed to start):

    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' );

    I know these are very different approaches, but I am stuck and don’t know what to do. Help would be very appreciated.

    Thanks a lot and with kind regards!

    Thread Starter vera1993

    (@vera1993)

    Hello Alexander,

    I included this code in an elementor child template in the function-php:

    function abfrage_meine_themen($query) {
    	if ($query->is_main_query() && is_user_logged_in()) { 
    		$ausgewaehlte_themen = get_field('meine_themen', 'user_' . get_current_user_id());
    		if ($ausgewaehlte_themen) { 
    			$tax_query = array( array( 'taxonomy' => 'db_themen', 'field' => 'slug', 'terms' => $ausgewaehlte_themen, ), ); 
    			$query->set('tax_query', $tax_query); } } } 
    add_action('pre_get_posts', 'abfrage_meine_themen');
    

    Than an error occured. In the backend the areas for pages and posts are broken and I get this message:

    So I deleted the code again and everything was ok again.

    Maybe you can help me some more?

    Here some details regarding my setup:

    The taxonomy looks like this (created with CPT UI):

    The ACF-Setting look like this:

    This is the result in the user profile page (in the end this show be editable throught the frontend):

    And here you can see some examples of entries in the Taxonomie “DB Themen” with the number of assigned post from the custom content type “Datenbank” (created with CPT UI):

    I hope this is helpful for solving my issue. I am very grateful for your kind help and support!

    Thread Starter vera1993

    (@vera1993)

    Thank you very much, I will try to add the code and give you a follow up on how it is going!

Viewing 3 replies - 1 through 3 (of 3 total)