4

I know it should work fine, don't know if this is a recent bug or something. So, to confirm it, I installed a fresh wordpress using default theme. I made a small plugin like bellow:

<?php
/*
Plugin Name: Test
Plugin URI: http://www.something.com
Description: To test
Version: 1.0
Author: Yet another dev
Author URI: http://me.com
*/

add_filter('post_type_link', 'wpse33551_post_type_link', 1, 2);

function wpse33551_post_type_link( $link, $post = 0 ){
    return home_url( 'temp-tests/' . $post->ID );
}

add_action('init', 'sfsafsdsf_test');

function sfsafsdsf_test(){
    wp_die(get_permalink(1));
}

But for some reason it's still showing:

http://example.com/2016/06/22/hello-world/

Let me know if I'm wrong at something.

Edit

My concern is that above code should output this (or let me know if I'm wrong at something):

http://example.com/temp-tests/1

When I said "output", I mean the permalink for postID #1 should be like that.

Edit 2

No, this isn't a custom post type. I'm using default "post" for this.

7
  • What are you looking to accomplish, what is your issue Jun 23, 2016 at 17:20
  • @PieterGoosen To put simple, I need to change permalink structure for specific post types
    – Rizwan
    Jun 23, 2016 at 17:21
  • Please file an edit and add all relevant info in your question. Important info in comments is useless as many do not read comments Jun 23, 2016 at 17:27
  • @PieterGoosen sorry about that. But I just need to know why 'post_type_link' isn't working even on a fresh installation. The posted code isn't my relevant code, I just came up with this to test it.
    – Rizwan
    Jun 23, 2016 at 17:33
  • 1
    @Rizwan, get_permalink is not calling get_post_permalink for built-in post types. Therefore the filtering never takes place the way you've set it up. Try using get_post_permalink instead, which is the function that post_type_link filters as stated in the codex.
    – Luis Sanz
    Jun 23, 2016 at 21:33

1 Answer 1

10

It is not a bug. As @LuisSanz pointed out, post_type_link isn't used for the built-in post post type. It is used for custom post types only.

Skim through get_permalink() function, you will find out that WordPress doesn't use post_type_link filter but uses post_link filter. It means that you're using wrong filter.

Then, change post_type_link to post_link should fix the problem:

add_filter('post_link', 'wpse230567_filter_post_link', 1, 2);

function wpse230567_filter_post_link($link, $post = 0)
{
    return home_url('temp-tests/' . $post->ID);
}
2
  • get_permalink does invoke the post_type_link filter, just not directly (and not for post post types). It returns get_post_permalink for anything that isn't a post, page or attachment, and that return is filtered through post_type_link. So you can totally use post_type_link for custom post types and post_type_link should behave exactly as expected.
    – indextwo
    Mar 12, 2018 at 18:39
  • Also for the pages, the filter is page_link. Thank you for the pointer @janevu. Jan 8, 2022 at 11:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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