4

I tried to find filters or a hook to modify link returned by this function

get_post_type_archive_link()

I have gone through most of the documentation. I'm not sure it's not there or I'm not able to find it.

Any suggestions would be very appreciated.

6

The filter is post_type_archive_link, defined (WP 4.1.1) on line 1112 of file wp-includes/link-template.php:

apply_filters( 'post_type_archive_link', $link, $post_type );

And the use:

add_filter( 'post_type_archive_link', function( $link, $post_type ) {

    //Do something

    return $link;

}, 10, 2 );
5

Just look at the source of get_post_type_archive_link():

return apply_filters( 'post_type_archive_link', $link, $post_type );

Sidenote: The function uses home_url(), which is a wrapper for get_home_url(), which offers another filter:

return apply_filters( 'home_url', $url, $path, $orig_scheme, $blog_id );

that runs earlier. With "pretty permalinks" enabled, it runs the following:

$link = home_url( user_trailingslashit( $struct, 'post_type_archive' ) );

and without:

$link = home_url( '?post_type=' . $post_type );
5

Welcome to WPSE Ittikorn!

Sometimes it is the easiest to just look right at the source. There even is a link right at the bottom of the Codex page you linked:

https://core.trac.wordpress.org/browser/tags/4.1.1/src/wp-includes/link-template.php#L1112

There you can easily see that there actually is a filter called post_type_archive_link

So to give you an example:

add_filter( "post_type_archive_link", "wpse_179209", 10, 2 );

function wpse_179209( $link, $post_type ){
    // Do whatever you want to the $link

    return $link;
}

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.