Skip to content

Instantly share code, notes, and snippets.

@CAWeissen
Last active July 16, 2016 23:14
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save CAWeissen/7d80f8c1fcf0df4b1777c351c073c8e1 to your computer and use it in GitHub Desktop.
Save CAWeissen/7d80f8c1fcf0df4b1777c351c073c8e1 to your computer and use it in GitHub Desktop.
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