D
D
Damaks2015-09-19 23:46:40
CMS
Damaks, 2015-09-19 23:46:40

Permalinks with rewrite_rule. The head is already broken. How?

Good evening.
I've been scratching my head over this for a long time, to no avail. It's time to finally solve it.
The default permalink settings in wordpress do not allow you to change URLs for custom post types and taxonomies. The plug-in "Types" used also does not have such functionality. A glimmer of hope came with the "Custom Post Type Permalinks" plugin, but it's not enough. I couldn’t understand his code, it’s just hell.
I'll outline the situation.
There is one custom post type and several custom taxonomies.

  • References to a custom type entry look like this:
    "http://site.ru/%тип поста%/%slug записи%"
  • Links to custom taxonomy:
    "http://site.ru/%таксономия%/%slug таксономии%"
  • One of these taxonomies is a hierarchical list of cities by type:
    "Россия" - "Москва и московская область" - "Мытищи"

The value of this taxonomy is selected by the user and stored in the session. Depending on this value, records are displayed in other custom taxonomies and search using a modified loop. Those. the same link "http://site.ru/%таксономия%/%slug таксономии%"displays different records for different session values.
It is necessary to change the links to the list of posts of custom categories by adding the city selected by the user (from the session, it is similar to one of the custom taxonomies). For example, the user has chosen Moscow as his city. Then:
from the link of the view it is
"http://site.ru/%таксономия%/%slug таксономии%"
necessary to make the link of the view:
"http://site.ru/%город%/%таксономия%/%slug таксономии%"

(or
"http://site.ru/%таксономия%/%slug таксономии%/%город%"
, not yet determined).
That is, for all other custom taxonomies, add the selected city to the link. The same is true for references to records of a custom type. How?
I tried to deal with "add_rewrite_rule", but there was no success. Yes, and sensible manuals have not found. And if I find it, then either I compose the regular expressions incorrectly, or the rule itself does not work.
As I wrote above, there is a nice plugin "Custom Post Type Permalinks". It allows you to insert the value of a custom taxonomy (in my case, the city) into the link as a ruler. But it doesn't fit for two reasons:
1) the city is only inserted into links to a custom record type. And I also need pages with posts of custom taxonomies.
2) the insertion of the city has a hierarchy, that is, there through / successively all the values ​​from the country, region, to the city. Preferably, only the selected option by the user (he can also select the area).
How to implement all this, who will tell you? My gratitude will not be bounds, I will call the adviser "God of WordPress".

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
Igor Vorotnev, 2015-09-20
@HeadOnFire

Such rewrire rules are not an easy thing, because, as you noticed, there is practically no documentation and lessons on this topic. About two years ago I did a similar thing with cities for one WP-based SaaS platform. As a result, he did, but he killed more than one night for this, the mosk almost broke. Now I don’t remember the details from the start, but the plus or minus logic is as follows:
1. add_rewrite_tag - create template tags %city% and so on
2. register new variables for query through the query_vars filter
3. create rewrite_rules
The most important thing is regular expressions that will be your rewrite tags (1) parse into query variables (2). That is, the resulting string will eventually be like index.php?post_type=XXX&taxonomy1=YYY&taxonomy1_term=ZZZ&taxonomy2=WWW&taxonomy2_term=UUU
And in general, for starters, do step 2 and make sure that everything works for you with "ugly urls", and only then - take care of CNC. Because with rewrite rules, most often the trouble is that the query vars are wrong.
Well, then the URLs of the links themselves (permalinks) will need to be built in the required format. There are some hooks, but it's not difficult anymore.
In general, get ready for a lot of debugging, stock up on coffee Query Monitor and Rewrite Rules Inspector to help.
If you persistently explore the results of Google after the 3rd page, try different key phrases, then you should find 2-3 lessons, more or less deep. There were definitely 2 pieces on some site, it seems with the word Shiba in the title. I'll rummage through the archives at my leisure, maybe I'll find it.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question