Function Reference: geodir_imex_get_posts

Summary

Retrieves the posts for the current post type.

Global Values

$wp_filesystem
(object) (required) WordPress FileSystem object.

Default: None

Package

GeoDirectory

Parameters

$post_type
(string) (required) Post type.

Default: None
$per_page
(int) (required) Per page limit.

Default: 0
$page_no
(int) (required) Page number.

Default: 0

Return Values

(array)
  • Array of posts data.

Change Log

Since: 1.4.6

1.5.1 Updated to import & export recurring events.

1.5.3 Fixed to get wpml original post id.

1.5.7 $per_page & $page_no parameters added.

1.6.11 alive_days column added in exported csv.

Filters

‘geodir_export_listing_csv_column_names’ [Line: 5203]

‘geodir_export_listing_csv_column_values’ [Line: 5421]

Source File

geodir_imex_get_posts() is located in geodirectory-admin/admin_functions.php [Line: 5090]

Source Code

function geodir_imex_get_posts( $post_type, $per_page = 0, $page_no = 0 ) {	
	global $wp_filesystem;

	$posts = geodir_get_export_posts( $post_type, $per_page, $page_no );

	$csv_rows = array();
	
	if ( !empty( $posts ) ) {
		$is_payment_plugin = is_plugin_active( 'geodir_payment_manager/geodir_payment_manager.php' );
		$location_manager = function_exists('geodir_location_plugin_activated') ? true : false; // Check location manager installed & active.
		$location_allowed = function_exists( 'geodir_cpt_no_location' ) && geodir_cpt_no_location( $post_type ) ? false : true;
		$neighbourhood_active = $location_manager && $location_allowed && get_option('location_neighbourhoods') ? true : false;
		$is_claim_active = is_plugin_active( 'geodir_claim_listing/geodir_claim_listing.php' ) && get_option('geodir_claim_enable') === 'yes' ? true : false;
		$is_events_active = function_exists('geodir_event_plugin_activated') ? true : false;
		$is_custom_posts_active = function_exists('geodir_custom_post_type_plugin_activated') ? true : false;
		
		$post_ypes = geodir_get_posttypes('array');
		
		$csv_row = array();
		$csv_row[] = 'post_id';
		$csv_row[] = 'post_title';
		$csv_row[] = 'post_author';
		$csv_row[] = 'post_content';
		$csv_row[] = 'post_category';
		$csv_row[] = 'default_category';
		$csv_row[] = 'post_tags';
		$csv_row[] = 'post_type';
		if ( $post_type == 'gd_event' ) {
			$csv_row[] = 'event_date';
			$csv_row[] = 'event_enddate';
			$csv_row[] = 'starttime';
			$csv_row[] = 'endtime';
			
			$csv_row[] = 'is_recurring_event';
			$csv_row[] = 'event_duration_days';
			$csv_row[] = 'recurring_dates';
			$csv_row[] = 'is_whole_day_event';
			$csv_row[] = 'event_starttimes';
			$csv_row[] = 'event_endtimes';
			$csv_row[] = 'recurring_type';
			$csv_row[] = 'recurring_interval';
			$csv_row[] = 'recurring_week_days';
			$csv_row[] = 'recurring_week_nos';
			$csv_row[] = 'max_recurring_count';
			$csv_row[] = 'recurring_end_date';
		}
		$csv_row[] = 'post_status';
		$csv_row[] = 'is_featured';
        // Export claim listing field
		if ($is_claim_active) {
			$csv_row[] = 'claimed';
		}
		if ($is_payment_plugin) {
			$csv_row[] = 'package_id';
			$csv_row[] = 'alive_days';
			$csv_row[] = 'expire_date';
		}
        $csv_row[] = 'post_date';
		$csv_row[] = 'post_address';
		$csv_row[] = 'post_city';
		$csv_row[] = 'post_region';
		$csv_row[] = 'post_country';
		$csv_row[] = 'post_zip';
		$csv_row[] = 'post_latitude';
		$csv_row[] = 'post_longitude';
        if ($neighbourhood_active) {
            $csv_row[] = 'post_neighbourhood';
            $csv_row[] = 'neighbourhood_latitude';
            $csv_row[] = 'neighbourhood_longitude';
        }
		$csv_row[] = 'geodir_timing';
		$csv_row[] = 'geodir_contact';
		$csv_row[] = 'geodir_email';
		$csv_row[] = 'geodir_website';
		$csv_row[] = 'geodir_twitter';
		$csv_row[] = 'geodir_facebook';
		$csv_row[] = 'geodir_video';
		$csv_row[] = 'geodir_special_offers';
		if ($is_events_active || $is_custom_posts_active) {
			$csv_row[] = !empty($post_ypes[$post_type]['linkable_to']) ? 'linked_' . $post_ypes[$post_type]['linkable_to'] . '_ID' : 'linked_cpt_ID';
		}
		// WPML
		$is_wpml = geodir_wpml_is_post_type_translated( $post_type );
		if ($is_wpml) {
			$csv_row[] = 'language';
			$csv_row[] = 'original_post_id';
		}
		// WPML

		$custom_fields = geodir_imex_get_custom_fields( $post_type );
		if ( !empty( $custom_fields ) ) {
			foreach ( $custom_fields as $custom_field ) {
				$csv_row[] = $custom_field->htmlvar_name;
			}
		}

		// Export franchise fields
		$is_franchise_active = is_plugin_active( 'geodir_franchise/geodir_franchise.php' ) && geodir_franchise_enabled( $post_type ) ? true : false;
		if ($is_franchise_active) {
			$csv_row[] = 'gd_is_franchise';
			$csv_row[] = 'gd_franchise_lock';
			$csv_row[] = 'franchise';
		}
        
        /**
         * Filter columns field names of gd export listings csv.
         *
         * @since 1.6.5
         * @package GeoDirectory
         *
         * @param array $csv_row Column names being exported in csv.
         * @param string $post_type The post type.
         */
        $csv_row = apply_filters('geodir_export_listing_csv_column_names', $csv_row, $post_type);
		
		$csv_rows[] = $csv_row;

		$images_count = 5;
        $xx=0;
		foreach ( $posts as $post ) {$xx++;
			$post_id = $post['ID'];
			
			$gd_post_info = geodir_get_post_info( $post_id );
			$post_info = (array)$gd_post_info;
			
			$taxonomy_category = $post_type . 'category';
			$taxonomy_tags = $post_type . '_tags';
			
			$post_category = '';
			$default_category_id = $gd_post_info->default_category;
			$default_category = '';
			$post_tags = '';
			$terms = wp_get_post_terms( $post_id, array( $taxonomy_category, $taxonomy_tags ) );
			
			if ( !empty( $terms ) && !is_wp_error( $terms ) ) {
				$post_category = array();
				$post_tags = array();
			
				foreach ( $terms as $term ) {
					if ( $term->taxonomy == $taxonomy_category ) {
						$post_category[] = $term->name;
						
						if ($default_category_id == $term->term_id) {
							$default_category = $term->name; // Default category.
						}
					}
					
					if ( $term->taxonomy == $taxonomy_tags ) {
						$post_tags[] = $term->name;
					}
				}
				
				if (empty($default_category) && !empty($post_category)) {
					$default_category = $post_category[0]; // Set first one as default category.
				}
				$post_category = !empty( $post_category ) ? implode( ',', $post_category ) : '';
				$post_tags = !empty( $post_tags ) ? implode( ',', $post_tags ) : '';
			}

			// Franchise data
			if ($is_franchise_active && isset($post_info['franchise']) && (int)$post_info['franchise'] > 0 && geodir_franchise_check((int)$post_info['franchise'])) {
				$franchise_id = $post_info['franchise'];
				$gd_franchise_info = geodir_get_post_info($franchise_id);

				if (geodir_franchise_pkg_is_active($gd_franchise_info)) {
					$franchise_info = (array)$gd_franchise_info;
					$locked_fields = geodir_franchise_get_locked_fields($franchise_id, true);
					
					if (!empty($locked_fields)) {
						foreach( $locked_fields as $locked_field) {
							if (isset($post_info[$locked_field]) && isset($franchise_info[$locked_field])) {
								$post_info[$locked_field] = $franchise_info[$locked_field];
							}
							
							if (in_array($taxonomy_category, $locked_fields) || in_array('post_tags', $locked_fields)) {
								$franchise_terms = wp_get_post_terms( $franchise_id, array( $taxonomy_category, $taxonomy_tags ) );
			
								if ( !empty( $franchise_terms ) && !is_wp_error( $franchise_terms ) ) {
									$franchise_post_category = array();
									$franchise_post_tags = array();
								
									foreach ( $franchise_terms as $franchise_term ) {
										if ( $franchise_term->taxonomy == $taxonomy_category ) {
											$franchise_post_category[] = $franchise_term->name;
										}
										
										if ( $franchise_term->taxonomy == $taxonomy_tags ) {
											$franchise_post_tags[] = $franchise_term->name;
										}
									}
									
									if (in_array($taxonomy_category, $locked_fields)) {
										$post_category = !empty( $franchise_post_category ) ? implode( ',', $franchise_post_category ) : '';
									}
									if (in_array('post_tags', $locked_fields)) {
										$post_tags = !empty( $franchise_post_tags ) ? implode( ',', $franchise_post_tags ) : '';
									}
								}
							}
						}
					}
				}
			}
						
			$post_images = geodir_get_images( $post_id );
			$current_images = array();
			if ( !empty( $post_images ) ) {
				foreach ( $post_images as $post_image ) {
					$post_image = (array)$post_image;
					$image = !empty( $post_image ) && isset( $post_image['path'] ) && $wp_filesystem->is_file( $post_image['path'] ) && $wp_filesystem->exists( $post_image['path'] ) ? $post_image['src'] : '';
					if ( $image ) {
						$current_images[] = $image;
					}
				}
				
				$images_count = max( $images_count, count( $current_images ) );
			}

			$csv_row = array();
			$csv_row[] = $post_id; // post_id
			$csv_row[] = $post_info['post_title']; // post_title
			$csv_row[] = $post_info['post_author']; // post_author
			$csv_row[] = $post_info['post_content']; // post_content
			$csv_row[] = $post_category; // post_category
			$csv_row[] = $default_category; // default_category
			$csv_row[] = $post_tags; // post_tags
			$csv_row[] = $post_type; // post_type
			if ( $post_type == 'gd_event' ) {
				$event_data = geodir_imex_get_event_data($post, $gd_post_info);
				$csv_row[] = $event_data['event_date']; // event_date
				$csv_row[] = $event_data['event_enddate']; // enddate
				$csv_row[] = $event_data['starttime']; // starttime
				$csv_row[] = $event_data['endtime']; // endtime
				
				$csv_row[] = $event_data['is_recurring_event']; // is_recurring
				$csv_row[] = $event_data['event_duration_days']; // duration_x
				$csv_row[] = $event_data['recurring_dates']; // recurring_dates
				$csv_row[] = $event_data['is_whole_day_event']; // all_day
				$csv_row[] = $event_data['event_starttimes']; // starttimes
				$csv_row[] = $event_data['event_endtimes']; // endtimes
				$csv_row[] = $event_data['recurring_type']; // repeat_type
				$csv_row[] = $event_data['recurring_interval']; // repeat_x
				$csv_row[] = $event_data['recurring_week_days']; // repeat_days
				$csv_row[] = $event_data['recurring_week_nos']; // repeat_weeks
				$csv_row[] = $event_data['max_recurring_count']; // max_repeat
				$csv_row[] = $event_data['recurring_end_date']; // repeat_end
			}
			$csv_row[] = $post_info['post_status']; // post_status
			$csv_row[] = (int)$post_info['is_featured'] == 1 ? 1 : ''; // is_featured
            if ($is_claim_active) {
                $csv_row[] = !empty($post_info['claimed']) && (int)$post_info['claimed'] == 1 ? 1 : ''; // claimed
            }
			if ($is_payment_plugin) {
				$csv_row[] = (int)$post_info['package_id']; // package_id
				$csv_row[] = $post_info['alive_days'] !== '' ? absint($post_info['alive_days']) : ''; // alive_days
				$csv_row[] = $post_info['expire_date'] != '' && geodir_strtolower($post_info['expire_date']) != 'never' ? date_i18n('Y-m-d', strtotime($post_info['expire_date'])) : 'Never'; // expire_date
			}
            $csv_row[] = $post_info['post_date']; // post_date
			$csv_row[] = stripslashes($post_info['post_address']); // post_address
			$csv_row[] = stripslashes($post_info['post_city']); // post_city
			$csv_row[] = stripslashes($post_info['post_region']); // post_region
			$csv_row[] = stripslashes($post_info['post_country']); // post_country
			$csv_row[] = stripslashes($post_info['post_zip']); // post_zip
			$csv_row[] = $post_info['post_latitude']; // post_latitude
			$csv_row[] = $post_info['post_longitude']; // post_longitude
            if ($neighbourhood_active) {
                $post_neighbourhood = '';
                $neighbourhood_latitude = '';
                $neighbourhood_longitude = '';
                if (!empty($post_info['post_neighbourhood']) && ($hood_info = geodir_location_get_neighbourhood_by_id($post_info['post_neighbourhood'], true, $post_info['post_location_id']))) {
                    if (!empty($hood_info)) {
                        $post_neighbourhood = $hood_info->hood_name;
                        $neighbourhood_latitude = $hood_info->hood_latitude;
                        $neighbourhood_longitude = $hood_info->hood_longitude;
                    }
                }
                $csv_row[] = stripslashes($post_neighbourhood); // post_neighbourhood
                $csv_row[] = $neighbourhood_latitude; // neighbourhood_latitude
                $csv_row[] = $neighbourhood_longitude; // neighbourhood_longitude
            }
			$csv_row[] = stripslashes($post_info['geodir_timing']); // geodir_timing
			$csv_row[] = stripslashes($post_info['geodir_contact']); // geodir_contact
			$csv_row[] = stripslashes($post_info['geodir_email']); // geodir_email
			$csv_row[] = stripslashes($post_info['geodir_website']); // geodir_website
			$csv_row[] = stripslashes($post_info['geodir_twitter']); // geodir_twitter
			$csv_row[] = stripslashes($post_info['geodir_facebook']); // geodir_facebook
			$csv_row[] = stripslashes($post_info['geodir_video']); // geodir_video
			$csv_row[] = stripslashes($post_info['geodir_special_offers']); // geodir_special_offers
			if ($is_events_active || $is_custom_posts_active) {
				$csv_row[] = !empty($post_info['geodir_link_business']) ? (int)$post_info['geodir_link_business'] : ''; // linked business
			}
			// WPML
			if ($is_wpml) {
				$csv_row[] = geodir_get_language_for_element( $post_id, 'post_' . $post_type );
				$csv_row[] = geodir_imex_original_post_id( $post_id, 'post_' . $post_type );
			}
			// WPML
			
			if ( !empty( $custom_fields ) ) {
				foreach ( $custom_fields as $custom_field ) {
					$csv_row[] = isset( $post_info[$custom_field->htmlvar_name] ) ? $post_info[$custom_field->htmlvar_name] : '';
				}
			}
			
			// Franchise data
			if ($is_franchise_active) {
				$gd_is_franchise = '';
				$locaked_fields = '';
				$franchise = '';
					
				if (geodir_franchise_pkg_is_active($gd_post_info)) {
					$gd_is_franchise = (int)get_post_meta( $post_id, 'gd_is_franchise', true );
					$locaked_fields = $gd_is_franchise ? get_post_meta( $post_id, 'gd_franchise_lock', true ) : '';
					$locaked_fields = (is_array($locaked_fields) && !empty($locaked_fields) ? implode(",", $locaked_fields) : '');
					$franchise = !$gd_is_franchise && isset($post_info['franchise']) && (int)$post_info['franchise'] > 0 ? (int)$post_info['franchise'] : 0; // franchise id
				}
				
				$csv_row[] = (int)$gd_is_franchise; // gd_is_franchise
				$csv_row[] = $locaked_fields; // gd_franchise_lock fields
				$csv_row[] = (int)$franchise; // franchise id
			}
            
            /**
             * Filter columns values of gd export listings csv file
             *
             * @since 1.6.5
             * @package GeoDirectory
             *
             * @param array $csv_row Field values being exported in csv.
             * @param array $post_info The post info.
             */
            $csv_row = apply_filters('geodir_export_listing_csv_column_values', $csv_row, $post_info);
			
			for ( $c = 0; $c < $images_count; $c++ ) {
				$csv_row[] = isset( $current_images[$c] ) ? $current_images[$c] : ''; // IMAGE
			}
			
			$csv_rows[] = $csv_row;

		}

		for ( $c = 0; $c < $images_count; $c++ ) {
			$csv_rows[0][] = 'IMAGE';
		}
	}
	return $csv_rows;
}