• Resolved Halit Ayarc?

    (@doktorramiz)


    Hello,

    I have a film blog. I use birectional fields to link the actors and films.

    When I tried to export the films, the film_actors field which is a relationship field exports an array instead of the names of the actors.

    I tried WP Imp Exp Lite and WP All Export plugins.

    I am an ignoramus about PHP by the way. This is a hobby blog.

    Does anybody have an idea how to solve solve this?

    Thanks

Viewing 2 replies - 1 through 2 (of 2 total)
  • Plugin Support Paul Clark

    (@pdclark)

    The below PHP, when installed as a plugin, will export post type “page” with IDs for relationships stored in field “film_actors”:

    <?php
    /**
     * Plugin Name: Pods — Relationship Export
     * Description: Export a post type to CSV with <a href="admin-ajax.php?action=export-relationship">this link</a>.
     * Plugin Author: ??δ???
     * Author URI: https://pd.cm/
     * Version: 1
     */
    
    add_action(
    	'wp_ajax_export-relationship',
    	function(){
    		global $post;
    
    		$post_type = 'page';
    		$relationship_field_key = 'film_actors';
    
    		header( 'Content-Type: text/csv' );
    		header( 'Content-Disposition: attachment; filename=' . $post_type . '.csv' ); 
    
    		$query = new WP_Query(
    			[
    				'post_type' => $post_type,
    				'posts_per_page' => -1,
    				'post_status' => 'publish',
    			]
    		);
    
    		ob_start();
    		$output = fopen('php://output', 'w');
    
    		$headers = [
    			'ID',
    			'post_title',
    			'post_content',
    			$relationship_field_key,
    		];
    
    		fputcsv( $output, $headers );
    
    		while ( $query->have_posts() ) {
    			$query->the_post();
    
    			fputcsv(
    				$output,
    				[
    					$post->ID,
    					$post->post_title,
    					$post->post_content,
    					implode( ',', (array) get_post_meta( $post->ID, $relationship_field_key, false ) )
    				]
    			);
    		}
    
    		fclose( $output );
    
    		echo ob_get_clean();
    
    		exit;
    	}
    );

    Once uploaded as a plugin, a link to download the CSV displays in the plugin description.

    Thread Starter Halit Ayarc?

    (@doktorramiz)

    Thank you Paul

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Exporting bidirectional relationship fields’ is closed to new replies.