• I have this function:

    function exclude_tagged_posts( $query ) {
        if ( $query->is_main_query()  && $query->is_home() ) {
            $query->set( 'tag__not_in', array( 404 ) );
        }
    }
    add_action( 'pre_get_posts', 'exclude_tagged_posts' );

    It excludes posts with a tag with ID = 404 from the search. Now, I would like to call a function from the address bar that only displays tags with ID = 404. How to do that? All I know is that you have to use a condition:

    is_home() && isset( $_GET['function_name'] )

    Right?

    • This topic was modified 4 years, 10 months ago by reti.

    The page I need help with: [log in to see the link]

Viewing 5 replies - 1 through 5 (of 5 total)
  • Thread Starter reti

    (@reti)

    Solved. It’s need to write “null” nstead of “404”.

    • This reply was modified 4 years, 10 months ago by reti.

    Your description of what that code does is not correct, or maybe I just interpret the word search differently. It is excluding posts with a tag having ID 404 from showing on the Latest Posts page.

    You are asking about a “function from the address bar”? Do you want a URL? or some code to find some posts?
    The standard pretty permalink for a tag page is domain/tag/term-slug/
    The code reference for WP_Query shows the parameter as tag_id and that code should be pulling it out of the URL so you don’t have to. But when I try a URL with tag_id=350 on my test site, it is ignored.

    Thread Starter reti

    (@reti)

    Hi,

    thanks for your answer. Look at this. These three posts with tag “gdgb” you can see only with “&show_excluded”. And these are two functions which do it:

    
    function exclude_tagged_posts( $query ) {
        if ( $query->is_main_query()  && $query->is_home() ) {
            $query->set( 'tag__not_in', array( 402 ) );
        }
    }
    add_action( 'pre_get_posts', 'exclude_tagged_posts' );
    
    function show_excluded_posts( $query ) {
        if ( $query->is_home() && isset( $_GET['show_excluded'] ) ) {
            $query->set( 'tag__not_in', array( null ) );
        }
    }
    add_action( 'pre_get_posts', 'show_excluded_posts' );
    

    It works well. Problem is solved, as I wrote. But when I call a function which reset all of facets (button “Alle zeigen”) from URLs with &show_excluded, the &show_excluded element is left. I don’t know how to get rid of it.

    • This reply was modified 4 years, 10 months ago by reti.
    • This reply was modified 4 years, 10 months ago by reti.
    Thread Starter reti

    (@reti)

    Thanks, I’ll check it and let you know.

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘isset, display excluded posts with a tag’ is closed to new replies.