How to place a meta value in a rewrite rule for a custom message type?

I completely rewrote this question because it was a little delayed, and I was worried that people missed it without having read it completely.

I have a custom message type (procedure) that has a custom meta key / value with the page id that I want to use as a bullet.

I use this function (below) to create permalinks in the administration area, but when viewing them, the page contains 404 errors. How to create rewrite rules to use this format?

function bv_procedure_parent_slug($url, $post) { if(get_post_type($post) == 'procedure' && get_post_meta($post->ID, 'procedure_parent', true)) { $procedure_parent = get_post(get_post_meta($post->ID, 'procedure_parent', true))->post_name; if($procedure_parent) { $url = str_replace('procedure', $procedure_parent, $url); } } return $url; } add_filter('post_type_link', 'bv_procedure_parent_slug', 1, 3); 

The goal here is that here I will have many posts in which there will be a meta key / procedure_parent => 31 (where 31 is the page identifier and the message name is “face”). When viewing a single entry, and not in the URL /procedure/facelift/ , I would like it to be /face/facelift/ .

For this, I believe that I need to have access to $ post when creating the rewrite rule so that I can use get_post_meta() .

But how?

+5
source share
1 answer

It seems that you have complicated your problem a bit. Would this work if you did the following?

  • Install the Custom Post Type Permalinks plugin and
  • Change the default permalink to http://example.com/custompostname/%category%/%postname%/ .
  • Then, instead of creating some pages with categories with them, use the functionality of the built-in WordPress archive and create your own template for your custom post.

Otherwise, I think add_permastruct() is a function that you don't see. I theorize this by considering a solution at https://wordpress.stackexchange.com/a/253325/58884 for a similar problem. Some code looks like this:

 function my_custom_rewrite() { add_permastruct('procedure', '/%parent%/', false); } add_action('init', 'my_custom_rewrite'); 

not 'rewrite' => array( 'slug' => '%parent%', 'with_front' => false ) in the message type declaration. The 'slug' => '%parent%' this declaration is an optional replacement for the name of the custom post type ( $post_type in register_post_type () codex entry ). So if you had 'rewrite' => array( 'slug' => 'wibblewobble', 'with_front' => true) , your URL would change from http://example.com/procedure/%postname%/ at http://example.com/wibblewobble/%postname%/ . With 'with_front' => false I don't think it does anything. When you entered 'slug' => '%parent%' , I think WordPress takes this as a string and does nothing dynamically with it.

Also use flush_rewrite_rules(); . You can use it in the init hooked function during testing, and then move it to the theme / plug-in activation function after it works.

Good luck

0
source

Source: https://habr.com/ru/post/1274634/


All Articles