1

I have a few custom post types whose default urls I would like to completely hide or disallow. So for example, I have a custom post type called organization, and the url without rewriting looks like this:

http://example.com/?organization=my-org-name

I have a fairly complicated and working rewrite structure in place that is a combination of the taxonomy tags and custom post type so that people can use urls like

http://example.com/AL/organization/my-org-name
http://example.com/CA/organization/my-org-name

etc, to get to the post in question. All that works fine. I just don't want anything to show up when one goes to the default URL. I started worrying about this when i noticed that generating XML sitemaps was somehow picking up the default. I want it gone gone gone, never found anywhere outside of my rewrite structure.

UPDATE:

I THOUGHT I just figured this out with adding:

  ,'publicly_queryable' => false

to my register post type code. This does prevent those pages from loading at least, but it does NOTHING to solve the dreaded urls showing up in XML sitemaps.

UPDATE 2:

I'm starting to wonder if I don't need to rewrite those default URLs to something else, but that would mean checking the url, doing a lookup, and then...returning just the first possible taxonomy tag??

  • 1
    are you filtering permalink output so the API generates the correct URLs? – Milo Feb 20 '17 at 18:49
  • If your only remaining concern is XML sitemaps, perhaps you should use a plugin that allows you to exclude content. For example, Yoast WordPress SEO lets you enable & disable archives on a per-post-type basis. – WebElaine Feb 20 '17 at 19:14
  • Thanks but I am using Yoast actually and disabling is not what I want to do, I just want my real urls reflected in the map. disabling the archives simply removes them from anywhere... – Stephen Feb 20 '17 at 19:17
  • @Milo I will look into filtering, not entirely sure how to apply it here, but I will investigate more, thanks for the link... – Stephen Feb 20 '17 at 19:18
0

OK, I finally figured it out. And I ended up having to compromise in case there is more than one location tag for the same item, but I am much happier with how it works.

FIRST, I had to change the permalink structure (thanks Milo) like so:

function get_real_link_please( $url, $post ) {
    if ( $post->post_type == 'organization' ){
    $loc_list = wp_get_post_terms($post->ID, 'my_locations');
    $location = $loc_list[0]->slug; //get first item in case there is more than one
    return home_url( $location . '/organization/' . $post->post_name, $url );
    }
    return $url;
}
add_filter( 'post_type_link', 'get_real_link_please', 10, 2 );

And THEN, I had to make sure my rewrite rule accounted for it:

add_action( 'init', 'cpt_rewrite_link' );

function cpt_rewrite_link(){
       add_rewrite_rule( '([^/]+)/([^/]*)/([^/]*)/', 'index.php?my_locations=$matches[1]&post_type=$matches[2]&name=$matches[3]', 'top' );

}

AND FINALLY (Very important!), I had to make sure that my cpt was in fact publicly queryable:

  ,'publicly_queryable' => true

The only thing that would make me happier is if I could find a way to have multiple permalinks for multiple locations, but since the other links will work, it is fine to have the first location be the "official" one. Hope this helps someone else, it was a bear.

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.