0

(My issue is not that described in 'post_type_link' filter not working. In that instance Rizwan was attempting to use post_type_link to filter posts, which are instead handled by post_link.)

In my case I'm trying to use post_type_link on a custom post type created by the Liquid Messages plugin. The post type is 'gc-sermons' and my code is as follows:

function lqd_message_link( $url, $post ) {
  if ( get_post_type( $post ) == 'gc-sermons' && isset( $wp_query->query_vars['messages-app-view'] ) ) {
    return add_query_arg( $_GET, $url );
    }
  return $url;
  }
add_filter( 'post_type_link', 'lqd_message_link', 10, 2 );

If you go to a sermon series page like the below:

https://liquidchurch.com/sermon-series/divine-direction/messages-app-view/

And then mouse over one of the messages you see the link URL is:

https://liquidchurch.com/message/step-by-step

Whereas I'm looking for it to be:

https://liquidchurch.com/message/step-by-step/messages-app-view/

Any ideas?

Note: The CPT (gc-sermons) is set during its registration with ep_mask as EP_ALL.

4
  • 1
    It looks like those are archives, not single posts, so that's not the right filter. Other than that, it works as you'd expect. If you go to an archive, the links include the GET parameters.
    – janh
    Commented Jan 23, 2018 at 8:22
  • 1
    What is the purpose of add_query_arg here? What the filter returns should be the same as the filters from your other questions.
    – Milo
    Commented Jan 25, 2018 at 17:11
  • Hi Milo! I was looking at this WP documentation which shows using add_query_arg: developer.wordpress.org/reference/hooks/post_type_link YOU ROCK! I changed it to follow the form you provided me elsewhere and it works BEAUTIFULLY!
    – davemackey
    Commented Jan 25, 2018 at 17:25
  • @Milo - So, one more little item. If a scroll to the bottom of the page I have navigation for "Older" and the link there appears as: liquidchurch.com/messages/messages-app-view/page/2 Which doesn't work. Is there a way for me to get this to work? Either by adding another endpoint or by somehow shifting the /messages-app-view to the end so it looks like: liquidchurch.com/messages/page/2/messages-app-view Thanks!
    – davemackey
    Commented Jan 25, 2018 at 17:44

1 Answer 1

0

Milo provided the answer in the comments above. I'm recording for posterity sake. :)

I was using add_query_arg and should instead have used return $url, so the code above should have looked like:

function lqd_message_link( $url, $post ) {
   global $wp_query;
   if ( get_post_type( $post ) == 'gc-sermons' && isset( $wp_query->query_vars['messages-app-view'] ) ) {
   return $url . 'messages-app-view/';
}
   return $url;
}
add_filter( 'post_type_link', 'lqd_message_link', 10, 2);

Your Answer

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

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