Support

Home Forums Event Espresso Premium Venue meta values query outside the loop

Venue meta values query outside the loop

Posted: January 23, 2018 at 6:54 am

Viewing 11 reply threads


Patrick

January 23, 2018 at 6:54 am

Hi EE Team,

I am trying to get, outside the loop, a venue meta value like this :
$telephone_org_resp = get_post_meta( $org_resp_id, 'vnu_phone', true );

where vnu_phone is the name of the meta field (grabbed in the chrome inspector) I am trying to get the value.

Am I missing something ?

Thanks in advance for your help.

Patrick


Josh

  • Support Staff

January 23, 2018 at 7:38 am

Hi Patrick,

The venue phone field isn’t actually stored in post meta. You can use the
espresso_venue_phone() template tag to get that field.

If you check the source:
https://github.com/eventespresso/event-espresso-core/blob/master/public/template_tags.php#L1099
you’ll see that the template tag echoes by default. So you can set then second parameter so it doesn’t echo.
e.g.
$telephone_org_resp = espresso_venue_phone( $org_resp_id, false );


Patrick

January 23, 2018 at 9:21 am

Thank you Josh,

But I have already tried this template tag function. Maybe it’s because I am trying to get an event field outside of my custom event template post ?

The following query works well (which I also need) :
$venue_phone = espresso_venue_phone( $post->ID, FALSE );

I am customizing the content-espresso_events.php which I have copied in my child theme. I need to get an additional venue meta in this template. Should I call back the EEH_Event_View object using something similar to this :
$event = EEH_Event_View::get_event( $event ); ?

Sorry, not sure 100% what I am doing at this time.


Josh

  • Support Staff

January 23, 2018 at 9:36 am

Maybe it’s because I am trying to get an event field outside of my custom event template post ?

Correct, because the template tag I shared with you will only work within a venue custom post type.

I need to get an additional venue meta in this template.

May I ask what exactly are you trying to get? Is it really meta data or some other field that’s included in the Venue editor?


Patrick

January 23, 2018 at 10:06 am

I am trying to get the venue phone of a venue that is not related to the current event post (indeed in the Venue editor). Here is a portion of my code inside content-espresso_events.php :

`
<div class=”content-box-blue”>

<?php $lieu_id = espresso_venue_id( $post->ID );

$lieu_category = get_the_terms( $lieu_id, ‘espresso_venue_categories’ );
$venue_phone = espresso_venue_phone( $post->ID, FALSE );

if ( $lieu_category[0]->slug == ‘organismes’ ) : ?>

<span><strong>Pour information :</strong></span><br/>

<span><?php the_field(‘nom_public_de_lorganisme’, $lieu_id);?></span><br/>
<span><?php the_field(‘adresse_postale’, $lieu_id);?></span><br/>
<span><?php the_field(‘ville’, $lieu_id);?>, </span>
<span><?php the_field(‘province’, $lieu_id);?></span><br/>
<span><?php the_field(‘code_postal’, $lieu_id);?></span><br/>
<span><strong>Courriel : </strong><?php the_field(‘courriel’, $lieu_id);?></span><br/>
<span><strong>Téléphone : </strong><?php echo $venue_phone;?> (boîte vocale)</span>

<?php elseif ( $lieu_category[0]->slug == ’emplacements’ ) : ?>

<?php $org_resp_id = absint ( get_field( ‘organisme_responsable’, $lieu_id ) ); ?>
<?php $telephone_org_resp = espresso_venue_phone( $org_resp_id, false ); ?>

<span><strong>Pour information :</strong></span><br/>

<span><?php the_field(‘nom_public_de_lorganisme’, $org_resp_id);?></span><br/>
<span><?php the_field(‘adresse_postale’, $org_resp_id);?></span><br/>
<span><?php the_field(‘ville’, $org_resp_id);?>, </span>
<span><?php the_field(‘province’, $org_resp_id);?></span><br/>
<span><?php the_field(‘code_postal’, $org_resp_id);?></span><br/>
<span><strong>Courriel : </strong><?php the_field(‘courriel’, $org_resp_id);?></span><br/>
<span><strong>Téléphone : </strong><?php echo $telephone_org_resp;?> (boîte vocale)</span>

<?php endif; ?>

</div>
`
The second callback of the template tag function (the elseif statement) does return any value. All other fields are from ACF.


Patrick

January 23, 2018 at 10:08 am

Sorry, for some reason, the triple backtick did’nt work in my previous reply…


Josh

  • Support Staff

January 23, 2018 at 10:14 am

We ask that code not be posted here, can you use a gist or a pastebin?

It’s not clear from your code, how are you getting the venue ID of the venue that is not related to the current event post?


Patrick

January 23, 2018 at 10:29 am

Sorry for the code.

I am trying to get the venue phone of a venue that is not related to the current post (single event view).

$telephone_org_resp = espresso_venue_phone( $org_resp_id, false);

Where $org_resp_id is the venue id of the unrelated event.


Tony

  • Support Staff

January 23, 2018 at 10:54 am

If you already have the original venue ID you can use our models to pull an EE_Venue object:

$venue = EEM_Venue::instance()->get_one_by_ID( $VNU_ID );
if ( $venue instanceof EE_Venue ) {
    echo EEH_Schema::telephone( $venue->phone() );
}

As you then have the EE_Venue object you also have access to any other information for that venue if needed.


Josh

  • Support Staff

January 23, 2018 at 11:05 am

How is it that you’re verifying the $org_resp_id is a valid venue ID? One thing that may be happening is the venue ID being passed into the espresso_venue_phone() tag might not be a valid venue ID, so it will help to at least add some validation e.g.:

if ( get_post_type( $org_resp_id ) == 'espresso_venues' ) {
    $telephone_org_resp = espresso_venue_phone( 955, false);
    echo $telephone_org_resp;
}


Patrick

January 23, 2018 at 11:45 am

Thanks Tony ! I knew intuitively I had to call the Venue model but did’nt dig enough ! Now working great ! I guest I can now get almost any EE fields I need to build all my templates. Regarding Josh comment on venue ID validation, I would like to say that I already made sure elsewhere that it is valid when trying to get the value. The problem is not with ID validation but with the fact that the template tag function espresso_venue_phone() does not work outside the event template, or should I say, outside the venue model.


Tony

  • Support Staff

January 24, 2018 at 3:15 am

The problem is not with ID validation but with the fact that the template tag function espresso_venue_phone() does not work outside the event template, or should I say, outside the venue model.

It does work outside of the event template and venue model.

The problem is happening because the global post object is an EE event that has a venue assigned to it.

espresso_venue_phone() uses EEH_Venue_View::get_venue() to pull the venue.

get_venue() checks the current post’s post type and if its an EE event pulls the venues assigned to that event and passes the first back (the models accommodate for multiple venues but there is no UI to add them to an event as of yet).

Because the post is an EE Event and has a venue assigned to it, that’s what you’re getting back on the second call to espresso_venue_phone().

I’ve created a ticket for our developers to investigate this but in the meantime, you can pull the EE venue object directly using the above 🙂

Viewing 11 reply threads

The support post ‘Venue meta values query outside the loop’ 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