Welcome Guest! Login? Checkout

Take a look at the WordPress Talks section of my site. On that section I generally list all the public talks I’ve done. Some have videos, some just have a slide and a brief introduction. I use this to remember what I’ve said and where. It isn’t wholly complete – a few talks have been missed off, but overall it’s a good record of my talks.

However, you may notice I do tend to repeat talks. That’s fine – contrary to WordPress guidelines, I’ve no problem giving the same talk in a few locations. Even so, I do try and update the presentation occasionally with new information, or when a new question comes to light.

This can lead to a problem, as sometimes the URL structure is not exactly the best – often the title doesn’t change and having [talk-name]-2/3/4/5 as a talk isn’t too great. I’m not expecting a huge SEO boost, but my theory is that with the meetup location in the title as well as the date, I may attract a few people searching for the talk and long tail searches for the date/location. That’s the plan anyway.

Adding it to the URL is a quick and easy process. However, I’d like to make it even quicker and easier. I want to do it automatically. So I fired up my code editor and wrote some code. Here’s how to include custom fields in WordPress permalinks.

The draft_to_publish Action

The first dive is to look at the actions run when the posts are published. There is a fair few detailed on the “Post Status Transitions” post on the WordPress codex, in the end I went with draft_to_publish. The reasons for this are twofold:-

  • 99% of the time, I will want the code running when we go from a post draft, to publishing the post.
  • You don’t have to rely on removing actions on the fly with some things such as save_post.

Another option would be to use {status}_{post_type} filter structure. In fact, that would probably be better, as I’ll reveal why a bit later, but for this example, we will use draft_to_publish.

Coming Up With a URL Structure

The next option would be to come up with a URL structure. As we’re only editing the permalink (or post slug) there doesn’t need to be too much work done with permalinks. The only thing is to possibly differentiate each post with a similar name.

The custom post type I built for the talks page has a custom field – “Location”. It will help define the event I speak at. The chances are I’m unlikely to speak the same talk at the same event twice, so I’ll add it.

If I forget to add the location, I may also want to add the date. After all, the chances of me giving the same talk on the same date is unlikely, so I’ll add the date – even if it’s just the day, month and year.

After originally writing the date as 3 numbers, I decided to instead write the date using the names (such as 1st December, 2018). People that search for the date are likely to use words, rather than numbers, so make it easier to find.

The Code

With all that said, here’s the final code. This is the code currently used on this blog, right this second.

The final line – wp_update_post, actually updates the post, and it works pretty well. However, it can be improved.

Improvements

This is only used as a quick fix for my plugin, and it can be improved, here’s my 3 ways I know it can be improved.

  1. Using talks_publish would run this whenever a talk is published.  This will immediately improve it as we don’t need to run a post type check, making it more efficient. You can see this on lines 10 & 11 in the code. Granted it’s not a huge speed improvement, but it’s something.
  2. You will need to save the post before publishing. This is so that the URL location is saved to the post. This may be improved if I use talks_publish instead of draft_to_publish.
  3. Finally, should you need to return the post to a draft, you will need to change the URL structure to remove the location & date string, before republication.

But overall, as a quick snippet that helps include custom fields in WordPress permalinks it works well! I’ll be talking about some of the Yoast SEO changes as well made as well on these pages in the next post.

 
 
 

Comments

Polite Disclaimer: I am welcome, open and willing for corrections to be shared in the comments (with corrections being added to posts and credited), and the comments field should be used to promote discussion and make this post better. I do not know everything and if anybody finds a better way to do something, then by all means please share it below.

However, I'm unable to offer support to posts. The reason being is that WordPress has tens of thousands of plugins and millions of themes. As such, finding out exactly why code doesn't work with your setup is a long process. If you wish for me to look at your code, please use the priority support area.

Comments asking support will be left on the site, but there is no guarantee of answer.

    Comments are closed.