Q
Q
Q1332018-02-19 00:54:49
WordPress
Q133, 2018-02-19 00:54:49

How to properly set up CNC specific urls for custom post types in WP?

Hello.
Just started learning WP. The task is to collect a catalog of organizations with a complex structure, filtering, CNC.
I made a set of my own post types (via register_post_type) and created a couple of test posts.
Conditional pair of them

  • Post type city with country taxonomy.
  • company record type

Next, I try to create the view rules:
add_rewrite_rule(
   "catalog/(ru|by|ua)/$",
   'index.php?post_type=city', //&country=$matches[1]
   "top"
 );

In general, the goal is to build a slightly wider coverage of the CNC:
#catalog/%country%/ - список городов
#catalog/%country%/%city%/ - список компаний, привязанных к конкретному городу
#catalog/%country%/%city%/%company%/ - конкретная компания

and many narrow filters
#catalog/%country%/%city%/filer_type/(ip|ooo)/ - вывод компаний отфильтрованных по типу ИП, ООО 
#catalog/%country%/%city%/filer_vid/([a-z_\-]+)/ - вывод компаний с фильтром по виду деятельности

there will also be general filters catalog/%country%/%city%/filter/?f1=x&f2=y&f3=z
I've already reached the add_rewrite_rule level , I've got
a number of questions:
1. setting up a new post type (register_post_type). When creating:
public - true, (to see in the admin panel), but what should be in the end
publicly_queryable
query_var
rewrite

So that:
I can set up my CNC rules via add_rewrite_rule, so that when accessing the site with the request
my.site/?city=moskva
, an empty page does not appear (i.e., so that a user request of this kind is not processed (if all 3 properties true - it is just processed)) and so that I can easily set my CNC rules
in
the future the request must be passed in the form
add_rewrite_rule(
        "^catalog/([a-z_\-]+)/$",
        'index.php?post_type=city&country=$matches[1]', 
        "top"
);

and then WP should select all the cities (post type city) that are in the country section (this is the taxonomy for city).
and will try to display them through archive-city.php, if it doesn't exist, then through archive.php?
After I add the add_rewrite_rule code to my function and hook it up
add_action( 'init', 'custom_rules' );
I save the settings on the page with the list of permalinks in the admin and the changes should take effect. But I stubbornly stalled
at the very first step: /catalog/ru/ - returns 404 =(
property created via the Extended Custom Fields plugin).
In theory, WP should understand index.php?post_type=city&post_id=$matches[1], but
this is also just my guess. And how to make him understand that he needs to output data through single.php according to the input variable?
3. I have not yet reached the filters in the CNC, so I don’t even really understand where to implement their logic in the end:
As I imagine it now - the logic is in the archive-XXX.php file, before the data is output.
XXX is determined by the post type that I end up with:
  • If the final data is a list of cities, then there will be a filter in archive-city.php that will focus on the data from get_query_var and prefilter the data.
  • If the target data is organizations, then archive-company.php will work with its own data filter.
    + because because there will be a bunch of different filter options, then there will be a pack of logic? Or is it possible to organize it in a normal way?

.
4. checking the url
+ format, you will also need to check the correctness of the CNC, for example, that at the address
/ru/tokio/some-firm/ - that the firm some-firm is really tied to the city of Tokyo through a custom property, and the city of tokyo - to the country RU.
Or is it possible to bypass this somehow with regular mechanisms?
Thanks for reading =)

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question