WordPress.org

Ready to get started?Download WordPress

Forums

Globally filtering posts by Custom Taxonomy (2 posts)

  1. zerfl
    Member
    Posted 3 years ago #

    Hello!

    On our site we want to add the possiblity for the user to choose the content (s)he would like to read about the most. Therefore, I added a simple form which displays all terms of our custom taxonomy (which is assigned to a custom content type).
    Once the user made his/her choice, the term ids are saved in a COOKIE.

    What I need now is a hook to change *all* queries to reflect the user's preference.

    Example:

    I have a custom content type called "News". This content-type has a custom taxonomy called "Flowers". The taxonomy has different terms (e.g. colours) like: Green, Blue, etc. On the front page the user chose to only see News (the content type) that have the term "Green" assigned to the taxonomy "flowers". This is now the global setting. Whenever a list of news is being displayed it filters out all entries that do not meet the requirements.

    Is this even possible like that?

    I tried finding some useful info on the "Custom Queries"-Codex page and also checked the pre_get_posts / found_posts hooks/filters to no avail.

    pre_get_posts sounds perfect, but can it work with custom taxonomies?

    Any help is much appreciated. Thanks!

  2. lulalala
    Member
    Posted 2 years ago #

    I have a similar situation, and I have a country texonomy. I have tried add_filter( 'request'.... This does filter out posts on the front page/search, but it does not for example filter out 'recent posts' widgets. Hope this might help, and I want to know if there is a way to cover widgets also. So...BUMP THREAD!.

    function alter_the_query( $request ) {
        $dummy_query = new WP_Query();
        $dummy_query->parse_query( $request );
    	$request['tax_query'] = array(
    		array(
    			'taxonomy' => 'country',
    			'field' => 'slug',
    			'terms' => 'US'
    		)
    	);
        return $request;
    }
    add_filter( 'request', 'alter_the_query' );

Topic Closed

This topic has been closed to new replies.

About this Topic