How to fix 404 on post after prepending Custom Post Type url with Custom Taxonomy Term slug

First, you don’t need to have two actions called during init, they can both be done under the same action function — so move the register CPT and taxonomy to a single function (or call separate functions from the main one) — no need to have two init actions. You should also be registering the … Read more

add_rewrite_rule wordpress ignoring url pattern, wp rewrite not working

You are trying to match 7-number-cities-starting-with-pa URL to ^([^/]*)-letter-words-starting-with- pattern in second rule. These rules should work: add_action( ‘init’ , ‘se336474_rewrite_rules’, 5 ); add_filter( ‘query_vars’, ‘se336474_query_vars’ ); function se336474_rewrite_rules() { // single pattern for URLs: // site.com/cities-starting-with-pa/ => start_letter=”pa” // site.com/7-cities-starting-with-pi/ => start_letter=”pi”, length=7 add_rewrite_rule( ‘^(?:([0-9]+)-)?cities-starting-with-([^/]*)/?$’, ‘index.php?page_id=340&start_letter=$matches[2]&length=$matches[1]’, ‘top’ ); // // OR for URLs: // … Read more

add_rewrite_rule only works when flush first

Problem with rules occurs, because rule for a given projecttype is added when you visit relevant URL. Every time you visit one of projects/{projectstype}/ addresses and flush rewrite rules, they are deleted and only one rule (only for current project type) is saved/created. You can add your code to request filter hook, then there is … Read more

Add slug to default post permalinks ONLY

Literally found the answer 10 minutes after posting this question and spending a couple of hours on it before asking. Simply hook your function to post_link! 🤦 public function __construct() { add_action(‘post_link’, [$this, ‘post_links’], 10, 2); } public function post_links($link, $post) { if ($post->post_type === ‘post’) { return site_url(‘slug/’. $post->post_name .’-‘. $post->ID); } else { … Read more

Rewrite rule can’t get the ID from rewrited

Ok, you need to add the id param to the list of public query variables : add_filter(‘query_vars’, ‘query_vars’); function query_vars($public_query_vars) { $public_query_vars[] = ‘id’; return $public_query_vars; } Thanks to this plugin for the help : https://wordpress.org/plugins/monkeyman-rewrite-analyzer/#reviews