0

I've been trying to figure out how to achieve a particular permalink structure to no avail. Here's the situation:

On my site, I have one main custom taxonomy (module). I have five custom post types (dispatch, topic, question, resource, activity). The "module" taxonomy is associated with each of the custom post types.

I want to achieve a permalink structure that looks like this (for an activity, for example):

http://example.com/module/%module%/activity/activity_name. And I want http://example.com/module/%module%/activity to go to the archive for the activity post type for those activities that are associated with %module%.

I've been using the PODS plugin to manage my custom post types/taxonomies. I've played around with a variety of custom code and other plugins to achieve my goal, but haven't gotten it to work.

Some of the posts on here that I've seen that are close to what I'm looking for involve writing the code to define the custom post types/taxonomies. I don't have a problem with that; I've done it before. However, since I have everything set up (and the custom structures are pretty extensive), I would like to continue to use/build on PODS, if possible.

1
  • note that as a 3rd party framework/plugin, PODs is offtopic here, you can ask general WP development questions but you can't ask about 3rd party plugins/themes/products/services. Any answer you get here may not be usable for your use case without abandoning PODs.
    – Tom J Nowell
    Commented Apr 11, 2023 at 16:56

2 Answers 2

0

It sounds like you want to create Custom Taxonomies with Pods. This is like creating a new option that you can later add to each item, like adding "Size" as an option to "Shirt". Then Size would be clickable, and you can see the archives for all similar sizes. This is done with Custom Taxonomies.

  1. In the wordpress backend, go to Pods Admin > Add New, and click Create New
  2. From Content Type drop-down, Select ‘Custom Taxonomy (like Categories or Tags)
  3. Enter Single & Plural Label (like Size and Sizes), use Advanced Options if you want to override the Archive Slug and hit Next Step.
  4. Configure Admin UI settings
  5. Configure Advanced Options, Hierarchal for Taxonomies like Categories, leave this alone for Tag style taxonomies.
  6. Configure Advanced Options, Associated Post Types to connect your Taxonomies to the Post Types they will organize.
  7. Add any Fields to the Taxonomy under Manage Fields.

Source: https://docs.pods.io/creating-editing-pods/creating-a-custom-taxonomy/


Side note: You may not need to have a permalink structure like you have mentioned. Using pods, you could have urls like: http://example.com/pod-module-name

For example:

http://example.com/question

That page would list all the question posts. And each individual post should have a url like:

http://example.com/question/the-question-slug/

This is the default behaviour of Pods. So it may already be doing something close to what you want.

3
  • Yes, I can get that permalink structure to work. Unfortunately, I need to be able to have archives for a particular set of any custom post type that is associated with a particular custom term. The site is academic, and the "module" taxonomy is being used to organize content like a class. I need to be able to have example.com/module/topic1/question/question-related-to-topic-1
    – mburtis
    Commented Apr 11, 2023 at 18:11
  • Sure thing - I've just updated my answer. hope it helps Commented Apr 12, 2023 at 17:19
  • I actually have a custom taxonomy in PODs -- "module" The issue is figuring out how to get the permalink structure I want to work with both that custom tax and the custom post types I've created (that all use that custom tax).
    – mburtis
    Commented Apr 12, 2023 at 17:47
0

I believe add_permastruct can help you here. For instance, let's assume the current permalink structure defined by PODs is "example.com/activities/activity_name".

function wpse415279_custom_permastruct(){
  add_permastruct( 
   'activities', /* needs to match current slug for "activities" post type */
   'module/%module%/activities/%activities%' 
  );
}

/* 
This function needs to run after PODs has defined the post type.
I assume it's doing that on the init hook, so this also gets hooked
to init but with a low priority of 100.
*/
add_action( 'init', 'wpse415279_custom_permastruct', 100 );

Now we tell Wordpress to change the links it generates on the front end to match our new permastruct above. We can do this by filtering the post type link:

function wpse415279_rewrite_activity_links( $post_link, $post_obj ){
  
  /* figure out if this post has a term in the 'module' taxonomy assigned to it */
  if ( is_object( $post_obj )){
    $terms = wp_get_object_terms( 
      $post_obj->ID, 
      'module' /* assuming 'module' is the key for your custom taxonomy */
    );
  }

  /* if it does, replace the rewrite tag %module% with the name of the module*/
  if ( $terms ){
    $post_link = str_replace('%module%', $terms[0]->slug, $post_link);
  }
  return $post_link;
}

add_filter( 'post_type_link', 'wpse415279_rewrite_activity_links', 10, 2 );

I made a mini version of your setup on my local machine with "modules" as a custom taxonomy and "activities" as a custom post type. This method worked for me in that context. Your setup is no doubt more complicated so I hope it can translate. It should fill the need of not requiring you to have defined the post type and taxonomy yourself. I just put the above code in a small plugin.

Also check out the top answer to this question if you haven't already, that's basically where I got this from.

Edit: Suppose I should mention to flush rewrites before testing by going to settings > permalinks and clicking "save changes".

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.