Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
This plugin allows Posts 2 Posts connections to be created and imported using Really Simple CSV Importer. This WordPress plugin requires Really Simple CSV Importer (https://wordpress.org/plugins/really-simple-csv-importer/) and Posts 2 Posts (https://wordpress.org/support/view/plugin-reviews/posts-to-posts)
<?php
/*
-------------------------------------------------------
Plugin Name: RSCSV/Posts 2 Posts
Description: Connect imported posts using Posts 2 Posts connections
Author: Chris Weissenberger, Aleksandr Beliaev
Version: 1.3
*/
add_filter('really_simple_csv_importer_post_saved', function($post) {
if (is_object($post)) {
//connected posts' slugs are delimited with ';' and imported as custom field in separate column with 'ministries_prayers' header
global $wpdb;
$ministries = $post->ministries_prayers;
$missionaries = $post->missionaries_prayers;
if (isset($ministries)) {
$min_slugs = explode(';',$ministries);
foreach ($min_slugs as $min_slug) {
$min_slug = preg_replace('/\./','-', $min_slug);
$min_id = $wpdb->get_var("SELECT ID FROM $wpdb->posts WHERE post_name = '$min_slug' AND post_type = 'ministries'");
// $debugtext = $debugtext.' // found spec slug:'.$min_slug.'; post ID: '.$h->postid().'; spec id:'.$min_id;
// $h->add_meta('ministries_prayers', $debugtext, true);
if ($min_id) {
// echo "<script>console.log('Post ID: ".$post['ID']."');</script>";
p2p_type( 'ministries_to_prayers' )->connect( $min_id, $post->ID, array(
'date' => current_time('mysql')
) );
}
}
}
// connected posts' slugs are delimited with ';' and imported as custom field in separate column with 'missionaries_prayers' header
if (isset($missionaries)) {
$miss_slugs = explode(';',$missionaries);
foreach ($miss_slugs as $miss_slug) {
$miss_slug = preg_replace('/\./','-', $miss_slug);
$miss_id = $wpdb->get_var("SELECT ID FROM $wpdb->posts WHERE post_name = '$miss_slug' AND post_type = 'missionaries'");
// $debugtext = $debugtext.' // found spec slug:'.$miss_slug.'; post ID: '.$h->postid().'; spec id:'.$miss_id;
// $h->add_meta('missionaries_prayers', $debugtext, true);
if ($miss_id) {
p2p_type( 'missionaries_to_prayers' )->connect( $miss_id, $post->ID, array(
'date' => current_time('mysql')
) );
}
}
}
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.