M
M
MrSusua2016-05-08 09:18:07
WordPress
MrSusua, 2016-05-08 09:18:07

Why doesn't URL rewriting work in WordPress?

Good afternoon!
There is a function that rewrites URLs:
from: site.ru/product/salat_cezar
to: site.ru/463
So, only the last rewrite rule (product_3) works in it. Why don't the first two rules (product and product_2) work? Why only the last rule works and the first two do not work?
The function itself:

function fun_798668( $link, $post = 0 ) {
  if ( $post->post_type == 'product' ) {
      return home_url( '/' . $post->ID );
  } elseif ( $post->post_type == 'product_2' ) {
      return home_url( '/' . $post->ID );
  } elseif ( $post->post_type == 'product_3' ) {
      return home_url( '/' . $post->ID );
  } else {
      return $link;
  }
}

add_filter( 'post_type_link', 'fun_798668', 1, 3 );




function fun_798686() {
  add_rewrite_rule(
    '([0-9]+)?$',
    'index.php?post_type=product&p=$matches[1]',
    'top'
  );
  add_rewrite_rule(
    '([0-9]+)?$',
    'index.php?post_type=product_2&p=$matches[1]',
    'top'
  );
  add_rewrite_rule(
    '([0-9]+)?$',
    'index.php?post_type=product_3&p=$matches[1]',
    'top'
  );
}

add_action( 'init', 'fun_798686' );

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry, 2016-05-08
@dimasmagadan

the rules work in order of priority - whoever got up first, that and sneakers.
when it comes to the first matching rule, execution ends.
you need to leave only one rule
like this, write
index.php?post_type=product_2,product_1,product_3& in it

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question