Title: Custom post types php function
Last modified: June 14, 2019

---

# Custom post types php function

 *  Resolved [Marieke_Louise](https://wordpress.org/support/users/marieke_louise/)
 * (@marieke_louise)
 * [6 years, 12 months ago](https://wordpress.org/support/topic/custom-post-types-php-function/)
 * Hello,
 * First of all, thank you for this great plugin!
 * We have developed a website that organises events for a remembrance in the Netherlands.
   There are over 120 events that need to be exported including their locations,
   in 1 export file. These are 2 different post types but I was able to do this 
   by wp_query:
 * `'post_type' => array('event','location'), 'post_status'=>'publish'`
 * However, I cannot get the events and locations together in a single <post>, the
   events and locations are seperate and I have been scratching my head to get this
   to work. I believe this can be achieved by custom PHP function but for some reason
   I am probably missing something obvious. I hope you can assist me.
 * The data is stored in 2 different database tables, for the events this is table
   em_events and for the locations this is em_locations. What I need is the info
   from the events and some of the info from the locations but in one single <post
   >
 * This is what I get (I have shortened it a bit):
 * For the events:
 * <post>
    <id>2722</id>
 *  <post_type>event</post_type>
    <_event_id>167</_event_id> **<_location_id>58</
   _location_id>** <_location_country></_location_country> <_location_region></_location_region
   > <_location_postcode></_location_postcode> <_location_state></_location_state
   > <_location_town></_location_town> <_location_address></_location_address> <
   _location_latitude></_location_latitude> <_location_longitude></_location_longitude
   > </post>
 * For the location:
 * <post>
    <id>1534</id> **<_location_id>58</_location_id>** <_location_country>
   NL</_location_country> <_location_region></_location_region> <_location_postcode
   >6811BP</_location_postcode> <_location_state>Gelderland</_location_state> <_location_town
   >Arnhem</_location_town> <_location_address>Velperbinnensingel 15</_location_address
   > <_location_latitude>51.9827007</_location_latitude> <_location_longitude>5.9134642
   </_location_longitude> </post>
 * All the info from the location is being exported fine but I need that info to
   be put in the event post above.
 * Is this even possible and can you give me a small push in the right direction?
 * Thank you and have a great weekend,
    Marieke
    -  This topic was modified 6 years, 12 months ago by [Marieke_Louise](https://wordpress.org/support/users/marieke_louise/).

Viewing 6 replies - 1 through 6 (of 6 total)

 *  Plugin Author [WP All Import](https://wordpress.org/support/users/wpallimport/)
 * (@wpallimport)
 * [6 years, 11 months ago](https://wordpress.org/support/topic/custom-post-types-php-function/#post-11643129)
 * Hi [@marieke_louise](https://wordpress.org/support/users/marieke_louise/)
 * > All the info from the location is being exported fine but I need that info 
   > to be put in the event post above. Is this even possible and can you give me
   > a small push in the right direction?
 * You would need to export the events only, then use PHP functions on the ‘_location_id’
   field to get the location information for each event.
 * Here is an example function:
 *     ```
       function my_get_location_info( $location_id, $field ) {
       	if ( ! empty( $location_id ) ) {
       		return get_post_meta( $location_id, $field, true );
       	}
       }
       ```
   
 * Example usage (in a Custom XML Export):
 * `<_location_country>[my_get_location_info({_location_id},'_location_country')]
   </_location_country>`
 * (_Note: If you’re using a simple XML export, using functions would be a bit different:
   [http://www.wpallimport.com/tour/export-developer-friendly/](http://www.wpallimport.com/tour/export-developer-friendly/)_).
 *  Thread Starter [Marieke_Louise](https://wordpress.org/support/users/marieke_louise/)
 * (@marieke_louise)
 * [6 years, 11 months ago](https://wordpress.org/support/topic/custom-post-types-php-function/#post-11645073)
 * Hi,
 * Thank you for your help. I have tried a similair code already but it is not working.
   The location ID is found in 2 seperate tables, if I use this code it only shows
   the location ID that is stored in the table EM_Events, the table EM_locations
   has the same location_ID and has all the data I need to retrieve and not in the
   EM_events table, where this code is trying to pull the data from that is not 
   existing. How can I ‘ jump’ to the table EM_locations and pull the meta frm there?
 *  Plugin Author [WP All Import](https://wordpress.org/support/users/wpallimport/)
 * (@wpallimport)
 * [6 years, 11 months ago](https://wordpress.org/support/topic/custom-post-types-php-function/#post-11663216)
 * Hi [@marieke_louise](https://wordpress.org/support/users/marieke_louise/)
 * > How can I ‘ jump’ to the table EM_locations and pull the meta frm there?
 * Is this a custom database table that’s created by your events plugin? If so, 
   you’ll need to use the WPDB class in your custom function to retrieve the data
   from it: [https://codex.wordpress.org/Class_Reference/wpdb](https://codex.wordpress.org/Class_Reference/wpdb).
 *  Thread Starter [Marieke_Louise](https://wordpress.org/support/users/marieke_louise/)
 * (@marieke_louise)
 * [6 years, 11 months ago](https://wordpress.org/support/topic/custom-post-types-php-function/#post-11692290)
 * Yes, it is a custom table that is added by Events Manager. So I have tried and
   tried and I can’t get it to work.
 * When I do SQL with the same select function in the database itself it works fine,
   but maybe I am missing something
 *     ```
       global $wpdb;
   
       	      $wpdb->get_results( SELECT <code>location_address</code> FROM sar_em_locations);
   
       function adres($location_address){
   
       	return $location_address;
       }
       ```
   
 * Still returns only the ID of the location and not the actual adress..
 *  Plugin Author [WP All Import](https://wordpress.org/support/users/wpallimport/)
 * (@wpallimport)
 * [6 years, 11 months ago](https://wordpress.org/support/topic/custom-post-types-php-function/#post-11697910)
 * Hi [@marieke_louise](https://wordpress.org/support/users/marieke_louise/)
 * We wouldn’t be able to help write this code or troubleshoot it, but here’s an
   example that should point you in the right direction: [https://paste.ee/p/tYqeZ](https://paste.ee/p/tYqeZ).
 *  Thread Starter [Marieke_Louise](https://wordpress.org/support/users/marieke_louise/)
 * (@marieke_louise)
 * [6 years, 10 months ago](https://wordpress.org/support/topic/custom-post-types-php-function/#post-11741081)
 * Brilliant, that is it!
 * I had tried a similar code, but with your help I could see what I’ve missed.
 * Thank you so much for the help, it is working perfectly!
 * Marieke

Viewing 6 replies - 1 through 6 (of 6 total)

The topic ‘Custom post types php function’ is closed to new replies.

 * ![](https://ps.w.org/wp-all-export/assets/icon-256x256.png?rev=2570162)
 * [WP All Export – Drag & Drop Export to Any Custom CSV, XML & Excel](https://wordpress.org/plugins/wp-all-export/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/wp-all-export/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/wp-all-export/)
 * [Active Topics](https://wordpress.org/support/plugin/wp-all-export/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/wp-all-export/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/wp-all-export/reviews/)

 * 6 replies
 * 2 participants
 * Last reply from: [Marieke_Louise](https://wordpress.org/support/users/marieke_louise/)
 * Last activity: [6 years, 10 months ago](https://wordpress.org/support/topic/custom-post-types-php-function/#post-11741081)
 * Status: resolved