• I made filter on post archives that shows only posts that match specified criteria in URL. So filter expression is in URL. That works fine in post archives of all kind.

    Problem I have is that when post is displayed and categories and tags this posts is attached to are listed but URLs of categories and tags are not containing specified filter so if user click on category or tag he gets all posts in that category or tag, not filtered.

    I spend lot of time searching for a solution. Basically, what I want is that filter expression on posts archive be reflected in URLs of post categories and tags displayed within that post archive.

    Can this be done?

    In code I do have all data about expression that should be applied to URL but I cannot find a way how to intercept and change url for each post category and tag before they are displayed.

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

    (@pedjas)

    I found https://developer.www.ads-software.com/reference/hooks/get_archives_link/ as it seems maybe should allow me to change URL of archive link, but I did not manage to make it work. It seems like filter is not triggered at all.

    add_filter("get_archives_link", "test_archives_link_filter");
    
    function test_archives_link_filter($link_html) {
    
      $link_html = "CHANGED URL";
      return $link_html;
    
    };
    
    
    

    I find it difficult to follow your description. I have an idea of what you’re getting at, but a practical example or even the complete code you used would be interesting. Especially with the latter you could recreate your problem and give you concrete tips, I think.

    Thread Starter pedjas

    (@pedjas)

    Sure I will try to make an example. thanks for taking interest.

    Lets say we have WP blog showing jokes.

    There are categories like: math jokes, sailor jokes, adult jokes and so…

    There are also number of tags.

    And there is custom taxonomy that describes each post if it is text joke, video joke, cartoon joke or photo joke (like memes). Idea is: user can choose what kind of jokes he likes to see and set that filter so he sees only them until he resets filter.

    So user opens category of math jokes, and all posts displayed are well – math jokes.

    But there is option for user to filter by custom taxonomy, so he chooses cartoon jokes. Filter expression is in some way incorporated in URL, for simplicity lets say it is query variable. URL is used as it allows passing filter around and also user can bookmark it.

    Now he gets posts from category math jokes but only cartoon jokes.

    On web page beside joke itself for each joke it is listed what all categories it belongs to, what tags are attached. This is where I stumbled ona problem. WP renders URLs/ for categories and tags using home url of a site. It does not know and does not include query in web page URL in rendered urls.

    I want to intercept it and change urls for categories and tags so when user clicks on any category or tag listed on page which is custom filtered to show only cartoons, he again gets only cartoons. It should work that way, until user removes his custom set filter.

    So far I can hook to some event that fires after whole HTML content is rendered and than use brute force to search all links ans replace them. But that is brute force.

    I hope there is way that I can intercept url creation for categories and tags so i can fix them there.

    For example, if I want to to the same to links of posts I can use hook ‘the_permalink’ and fix it there (i tried it, works fine). But I cannot find similar hook that works for categories, tags or any other taxonomy in general.

    Thread Starter pedjas

    (@pedjas)

    Here is an examle how it works with post link

    add_filter('the_permalink', 'force_type_permalink_filter');

    function force_type_permalink_filter ($url) {
    global $current_type_filter;
    return force_url ($current_type_filter, $url);
    }

    $current_type_filter is variable where I have current filter definition

    force_url() is function that alters provided URL with provided filter definition.

    I cannot find analog way to do this with urls for categories, tags ond other taxonimies.

    i tried using ‘get_archives_link’ instead of ‘the_permalink’ but that did not work. It seems like ‘get_archives_link’ hook is not trigerred at all.

    • This reply was modified 8 months, 3 weeks ago by pedjas.
    • This reply was modified 8 months, 3 weeks ago by pedjas.
    • This reply was modified 8 months, 3 weeks ago by pedjas.
    Thread Starter pedjas

    (@pedjas)

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘How to customize Category and Tag URL on Post?’ is closed to new replies.