1

I am trying to build a speaking URL for a custom post type in Wordpress storing event dates. I would like to have the following scheme for event-URLs:

/events/2013-12-24-christmas/ /events/2013-12-31-newyearseve/

The date is not the standard published date, it is instead a date stored in a meta-field: the date on which the event takes place.

I tried to achieve it with a snippet I used to rewrite another custom post type and it looked promising - but the only problem is: I can't get the post-id to query for the meta values :(

Here's the rewrite code so far:

function create_post_type_events {
  [...custom-post-type...]

  register_post_type('my_events', $args);

  global $wp_rewrite;
  $permalink_structure = '/events/2013-12-24-%my_events%';
  $wp_rewrite->add_rewrite_tag("%my_events%", '([^/]+)', "my_events=");
  $wp_rewrite->add_permastruct('my_events', $permalink_structure, false);
}
add_action('init', 'create_post_type_events');

The date is obviously hard-coded and needs to be replaced.

I tried $post, $wp_query, globalizing, get_queried_object()... nothing seems to help.

So, how (and is it actually possible?) can I get the post-ID for further querying?

lorem monkey
  • 3,942
  • 3
  • 35
  • 49
  • This is interesting. At first glance you want the date to NOT have a trailing '/' before the postname, only a dash '-', correct? Because my CPT's always default to /posttype/postname. Also, I have only seen post id's get meta data with $wpdb and a custom query like the way geo-data-store does it. I will be watching this post. – Ben Racicot Aug 12 '13 at 18:05
  • Can't you adjust the slug for this, that would make it so much easier. And a slug is meant for url's – janw Aug 12 '13 at 18:29
  • @BenRacicot Sorry for the misunderstanding, but the trailing slash is not the issue here ;) I edited my question as I have seen I missed a slash. – lorem monkey Aug 13 '13 at 07:07
  • @janw I know I could set the slug to represent this, but in this case the date strongly belongs to the title, it's the date of the event. I thought it would be better to have the event date and event title together. – lorem monkey Aug 13 '13 at 07:10
  • Okay I get that, but still setting the *title* part and the *date* part separate will help to get the *title* if that is the slug rather then a postmeta. And even if you merge *title* and *date* in a slug, that's what it is for. – janw Aug 13 '13 at 08:04
  • But apart from that - how to get the date or another value from a meta field? Even if the date *would* be the slug - how to get it? (It's not the posts published date!) – lorem monkey Aug 13 '13 at 08:32
  • Yes! I understand now. This is a truly unique and valuable question and maybe a bounty could be set? – Ben Racicot Aug 13 '13 at 13:06
  • I need to get the answer to this too, as I'm having the same problem and can't fix it - https://stackoverflow.com/questions/48209035/how-can-i-place-a-meta-value-in-rewrite-rule-for-custom-post-type – Lee Jan 18 '18 at 11:27

0 Answers0