Support

Home Forums Event Espresso Premium Append Data (i.e Custom Field) to Event URL

Append Data (i.e Custom Field) to Event URL

Posted: May 19, 2015 at 5:59 am

Viewing 5 reply threads


Dan

May 19, 2015 at 5:59 am

Hey guys –

Curious if there’s an easy answer for this or if it’s been asked before and I’m just not finding it on the forums. If not, I’d be happy to open a priority support ticket.

My site will host multiple events with the same Title (“Title of Class”). I have a custom field set up on each Event and I would like to append the value of this field to the URL of my event on Publish/Update – allowing these events to share titles and pretty slugs while maintaining a unique URL.
i.e: “events/title-of-class/custom1″,”events/title-of-class/custom2”
OR
“events/title-of-class-custom1”, “events/title-of-class-custom2”

What do you think is the best way of doing this?

Thanks,

Phil


Josh

  • Support Staff

May 19, 2015 at 8:27 am

Hi Phil,

WordPress has a function that allows you to add custom arguments to a query. Its name is add_query_arg().

Here are a few references that show how to use add_query_arg:

https://developer.wordpress.org/reference/functions/add_query_arg/
https://pippinsplugins.com/the-add_query_arg-helper-function/

Here’s an example that shows how you can use add_query_arg to add a custom field (‘query_arg’) to the event post’s permalink:

function ad_append_query_string( $url, $post ) {
    if ( 'espresso_events' == get_post_type( $post ) ) {
        if ( $meta = get_post_meta( $post->ID, 'query_arg', true ) ) {
            $url = esc_url( add_query_arg( 'data', $meta, $url ) );
        }
    }
    return $url;
}
add_filter( 'post_type_link', 'ad_append_query_string', 10, 2 );


Josh

  • Support Staff

May 19, 2015 at 8:29 am

One more idea, please see this stackexchange thread for an alternative solution:

http://wordpress.stackexchange.com/questions/127141/how-to-get-pretty-urls-with-add-query-arg-in-permalinks


Dan

May 19, 2015 at 11:16 am

Thanks for the detailed response. I think I follow what is going on here…

The function works as suggested, BUT…

The slug of my similar event still appends the default serialization. (screenshot)

I would like to have multiple events with the same title, yet unique URLs, without requiring an admin to edit the slug. Does that make sense?

My preference would be to append the custom field to the default slug without the variable syntax. (ie /events/my-event-title/custom-field)

I would be open to customizing the automatic slug behavior (ie automatically generate a slug of “/events/my-event-title-custom/” for the event My Title) if that is a better option.


Dan

May 19, 2015 at 11:18 am

I forgot to link the screenshot.
https://drive.google.com/open?id=0ByOfss3eDI2ad2thY0pSQk1XNU0&authuser=0


Josh

  • Support Staff

May 19, 2015 at 12:15 pm

Phil,

You’re looking at overriding core WordPress functionality, which isn’t something Event Espresso itself overrides. So the solution you’re looking for could also apply to regular WP posts and pages. I can recommend reviewing the code in the wp_unique_post_slug() function to see how the -2 gets added in the first place before you take a crack at removing it. Here’s a link to that function in WP Core trac:

https://core.trac.wordpress.org/browser/tags/4.2.2/src/wp-includes/post.php#L3705

The solution I linked to above might work around that functionality. You could look into that idea.

Or you can also use the wp_unique_post_slug filter on the function of the same name.

You can see in WP core where it lets you filter what gets returned for the post slug. So instead of using add_query_args() to add a parameter, you could use that filter to append your custom unique slug key.

Viewing 5 reply threads

The support post ‘Append Data (i.e Custom Field) to Event URL’ is closed to new replies.

Have a question about this support post? Create a new support post in our support forums and include a link to this existing support post so we can help you.

Event Espresso