4

i have a custom post type project

the url that worked out of the box was:

www.myapp.com/project/POST-ID

and

www.myapp.com/project/POST-SLUG

now my client needs the url be available in the root address. So I added the following code to functions.php

function na_remove_slug( $post_link, $post, $leavename ) {

  if ( 'project' != $post->post_type || 'publish' != $post->post_status ) {
    return $post_link;
  }

  $post_link = str_replace( '/' . $post->post_type . '/', '/', $post_link );

  return $post_link;
}
add_filter( 'post_type_link', 'na_remove_slug', 10, 3 );


function na_parse_request( $query ) {


  if ( ! $query->is_main_query() || 2 != count( $query->query ) || ! isset( $query->query['page'] ) ) {
    return;
  }

  if ( ! empty( $query->query['name'] ) ) {
    $query->set( 'post_type', array( 'post', 'project', 'page' ) );
  }
}
add_action( 'pre_get_posts', 'na_parse_request' );

this works now:

www.myapp.com/POST-SLUG

this does not work though:

www.myapp.com/POST-ID

is there any way I can make it work?

  • This going to result in conflicts with your pages permalinks as I tried to explain here before: wordpress.stackexchange.com/questions/203951/… – Jan Beck May 5 '16 at 11:54
  • @JanBeck I know, that is why I don't allow the users to use slugs that are used in different post types / pages. – Toskan May 6 '16 at 16:12
1

Try creating a custom rewrite rule and a custom rewrite tag. This should allow you to set up a regular expression to capture the ID and manipulate the query.

Rewrite API/add rewrite rule « WordPress Codex https://goo.gl/kdEJd8 Rewrite API/add rewrite tag « WordPress Codex https://goo.gl/PVLzLh

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.