0

I have a custom taxonomy, called filter. All of the following urls lead to the archive.php and should give me at least 1 post.

  • /filter/bar works by default, no custom rule needed
  • /myCategory/filter/bar works with my own custom rule, nice!
  • /search/foo works by my custom rule

  • /?s=foo&filter=bar works as expected

  • /search/foo/filter/bar BOOM! No entries

Here are my rules:

add_filter("rewrite_rules_array", function($rules) {

    $newRules = array();

    // ... more rules

    $newRules["search/(.+)/?$"] = 'index.php?s=$matches[1]';

    $newRules["search/(.*)/filter/(.*)/?$"] = 'index.php?s=$matches[1]&filter=$matches[2]';

    $merged = array_merge($newRules, $rules);

    return $merged;
});

EDIT

Query-Monitor for the desired URL (suche = search in german)

enter image description here

3
  • 2
    Try using wordpress.org/plugins/query-monitor which will tell you which rewrite is being matched. – Slam Dec 11 '17 at 21:05
  • Thank you for the tip– I edited my post and added a screenshot of a query-monitor analysis of the situation. Appreciate some further help! – kater louis Dec 12 '17 at 9:02
  • Jeez– big thanks for this plugin; huge helper in general; quick question though: I can't make much sense of the MySQL queries; is there a way to see what $wp_query looks like? or see all the query vars? – kater louis Dec 12 '17 at 15:24
1

With Slam's kind tip to use query-monitor plugin, I could figure out what the problem was.

Apparently the url /search/foo/filter/bar also fits the "general search" rewrite-rule. Makes sense, thinking about it– (.+) technically matches /foo/filter/bar aswell. So I could improve (.+) by excluding /s?

But I found an easier solution!

In hope "first match wins" I declared the more specific rule before the general rule– and e voila, it worked!

add_filter("rewrite_rules_array", function($rules) {

    $newRules = array();

    // more specific rule first
    $newRules["search/(.*)/filter/(.*)/?$"] = 'index.php?s=$matches[1]&filter=$matches[2]';

    // general rule later
    $newRules["search/(.+)/?$"] = 'index.php?s=$matches[1]';

    $merged = array_merge($newRules, $rules);

    return $merged;
});
2
  • I thought that would be the problem. Glad Query Monitor worked out for you! – Slam Dec 13 '17 at 16:17
  • Yeah it did! Thanks very much :) – Can you tell me if there is a way to see the actual $wp_query with Query-Monitor? I can't make that much use of the DB queries, to be honest :o – kater louis Dec 14 '17 at 16:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.