The post_type_link
Hook in WPFTS
The post_type_link
hook in WordPress is a filter that allows modification of the URL generated for custom post types. WPFTS utilizes this hook to apply Smart Excerpts to links of custom post types displayed in search results. The logic is similar to the attachment_link
and page_link
filters.
What WPFTS Does When post_type_link
is Called:
-
Condition Check: WPFTS checks if the following conditions are met:
- The plugin is active and the Smart Excerpts option is enabled (
is_smart_excerpts
). - The request is on a search results page (
is_search()
). - The request is not from the admin panel (
!is_admin()
). - The link is displayed within the WordPress loop, or in a Gutenberg block displaying the post title (
in_the_loop()
). - Or, if
$wpfts_core->forced_se_query
is not equal tofalse
(forced Smart Excerpts call).
- The plugin is active and the Smart Excerpts option is enabled (
-
Applying Smart Excerpts to the Link: If all conditions are met, WPFTS creates a
WPFTS_Result_Item
object for the post and calls itsTitleLink()
method, which returns the modified link.
Important Functions Involved in post_type_link
Processing:
WPFTS_Core::get_option()
is_search()
is_admin()
in_the_loop()
WPFTS_Result_Item::TitleLink()
WPFTS_Core::ForceSmartExcerpts()
(checks the value of$wpfts_core->forced_se_query
)
How to Use This in Addon Development:
Addon developers working with custom post types should be aware that WPFTS might modify links to these posts in search results. If your addon also modifies post links, ensure your logic doesn’t conflict with WPFTS.
Additional Notes:
- Smart Excerpts are applied to post links only in search results.
- The
post_type_link
filter is called for each custom post type displayed on the search results page. - WPFTS adds an additional check for compatibility with the Gutenberg block editor.