Home Forums General Issues How to display a wp post from a custom field trigger in custom posts (events)


How to display a wp post from a custom field trigger in custom posts (events)

  • I have event calendar. It has events posts. I inserted a relationship custom field which is displayed in events editor whereby I can click on a standard post to form a relationship. How do I display the post that this custom field page_link has triggered?
    Sorry I’m a non-programmer but can probably hack my way through it if someone is able to give proper direction – like where to put a hook/code, what function php to put it in, etc..

    I am desperate as my events site is live now and no one can see the posts from the calendar – only event posts which are non-themed styled.

  • If you’ve selected ‘Post IDs’ as the return format for your Relationship field, then get_field('page_link') (for example) will return an array of post IDs. If you clamp them down to just choosing ONE (by setting the Maximum Posts’ value to 1), then you can safely get your post ID by doing something like:

    $linked_post_ids_array = get_field('page_link'); // gets the array post post IDs
    $desired_post_id = $linked_post_ids_array[0]; // gets the ID of the 1st item in the array

    once you have the post ID, you can use a variety of WordPress and ACF functions to get related content,

    i.e. for an ACF field connected to the post, just pass the post ID:

    get_field('my_post_field', $desired_post_id);

    for general post info, you can use WordPress’ get_post

    $post_object = get_post($desired_post_id);

    hope this is enough info to point you in the right direction

  • I’m not quite getting it.

    In my WP I have two post types – regular posts (WordPress) and event posts (from The Event Calendar pro)

    Ive selected a ACF custom field in Custom Fields tab/FieldGroups (given field group a name ‘event_tribe_link’). Ive made a field also called ‘event_tribe_link’, it’s a relationship Field type and Ive checked Post ID as its return format. Maximum = 1. Ive elected Post type as Post (not all or anything else), Filter from Taxonomy is set to All with Post Title and Post Type Checked under ‘Elements’. Conditional logic = No, Location Rule is set to show field if; Post Type is equal to tribe_events.
    So hopefully the above is ok so far.

    Now when I go to the Event tab in WP and check an event post of say titled “XYZ Event’ I see the field ‘event_tribe_link’ appear followed by a ‘search..’ entry box and below it is a list of all my standard WP posts on which I now can either pick or search and link to the matching post I want to display – in this case Ive selected my related post to be ‘XYZ Post’. Doesn’t this now have the single one-to-one relationship already established or do I need your above code to return the post ID?

    Anyway, so all good so far.

    Now this is where I get confused. Where would I write your function example and in which php file would I add these statements (if there is a particular one from theme or The Event Calendar Pro)? Should I write these additional statements you suggest to return the post (from the ID relationship Ive selected) so that when I click on the calendar (which would normally take me to the ‘XYZ Event’ and display it) it takes me to the XYZ Post instead – and displays it?

  • “Doesn’t this now have the single one-to-one relationship already established or do I need your above code to return the post ID?”

    What happens is that the relationship field stores an array of the selected Post IDs as meta data against the Event post. Since you’ve clamped the maximum number of posts to just one, there will only be one item in the array, but it’s still an array, as opposed to a single value. So, yes, you’d need code like the above, in order to pull get the related Post ID. The code basically pulls out the value against the key of ‘0’ from the array

    From that point on, I’d imagine you have two options. You could either use that Post ID to pull IN data from the related Post, and feed it into your single Event template, or you could use the Post ID to determine where to redirect someone when they click on the item in the calendar (so instead of seeing a standard event calendar item, they’re actually bounced to the Post that you made the relationship with).

    Looks like you want the latter – bounce to the Post

    From this point on, it’s really an Events Calendar Pro issue – something I’m not that familiar with – but I seem to remember that you could create custom versions of the standard template, so you could override the default output. What you’re looking to do is determine whether a particular Event post has a relationship to a standard Post, and if it does, change the target URL to that post, rather than the standard single Event post.

    Have a look here:

    Looks like you need to find the file that contains the link you want to change (single-event.php for each of the different views you’re going to use), and where it says something like <a href="<?php tribe_event_link($post); ?>" class="url"> (which I think is the link from the calendar view to the single event), you’ll have to get in there and use an IF statement to intervene, so that if there’s a related event, you use get_permalink() and the related Post ID we talked about earlier, to bounce the user to the related post instead

    instead of:

    <h3 class="tribe-events-month-event-title summary"><a href="<?php tribe_event_link($post); ?>" class="url"><?php the_title(); ?></a></h3>

    maybe something like (expanded for clarity):

        <h3 class="tribe-events-month-event-title summary">
        <?php if ($desired_post_id) { ?>
        	<a href="<?php echo get_permalink($desired_post_id); ?>" class="url">
        <?php } else { ?>
        	<a href="<?php tribe_event_link($post); ?>" class="url">
        <?php } ?>
        <?php the_title(); ?></a></h3>

    where $desired_post_id is the related ID we obtained earlier.

    Hope this is useful. It’s probably more of an Event Calendar Pro puzzle from this point on

Viewing 4 posts - 1 through 4 (of 4 total)

The topic ‘How to display a wp post from a custom field trigger in custom posts (events)’ is closed to new replies.