• vera1993

    (@vera1993)


    Hello everyone,

    I have a website with a taxonomy called “db_themen” with different values. The posts from from my webiste are assigned to these taxonomy values from “db_themen”.

    On my website, a user can choose in his frontend user profile which “db_themen” he is interested in and save this input. This works through an ACF field of the type “Taxonomy” (field name: “meine_themen”), which is linked to the taxonomy “db_themen”.

    According to his input, only posts that are assigned to the same taxonomies are supposed to show on the frontend archives (Elementor archives Loop Grid) for this user.

    I tried to code something within my functions.php, but I am a total beginner. The only thing this code does is hide all posts entries in the backend:

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

    Can someone please tell me where my mistakes are? I am sure there are several ??

Viewing 5 replies - 1 through 5 (of 5 total)
  • Alexander

    (@alextonio22)

    Hi vera1993,

    Your code seems close to achieving your goal, but a couple of adjustments are needed. It appears that the issue lies in how you’re constructing the tax query and checking the ACF field values. Let’s make some modifications to address this:

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

    Here are the changes:

    1. Adjusted the condition to check if terms are selected using if ($ausgewaehlte_themen) to ensure it proceeds only when there are selected terms.
    2. Removed unnecessary curly braces for better code structure.

    Make sure to confirm that you have the correct ACF field name (‘meine_themen’) and taxonomy name (‘db_themen’) in your code. Also, ensure that the ACF field is configured to return term slugs. If you encounter any issues, double-check your ACF settings and verify that your taxonomy and field names are accurate.

    If this doesn’t work as expected, please provide more details or a screenshot of your ACF field (‘meine_themen’) configuration and the taxonomy (‘db_themen’). This will help me analyze the issue more accurately and provide a precise solution.

    Best regards,

    Alexander

    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!

    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!

    Moderator bcworkz

    (@bcworkz)

    Please check your error log to find out what actually caused the error message your’re seeing (Es gab einen…)

    I don’t know ACF very well, but I’m suspicious of how you pass the second arg to get_field(). ACF docs indicated this should be a post ID, not a string like “user_123”. I don’t even know if get_field() can be used to get user meta data or if it’s just for post meta.

    I assume ACF saves additional user field data in user meta. If so, you could instead do:
    $ausgewaehlte_themen = get_user_meta( get_current_user_id(), 'meine_themen', true );

    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!

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Show Post based on taxonomies choosen by the user’ is closed to new replies.