Skip to content

Instantly share code, notes, and snippets.

@wkw
Created May 5, 2014 17:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wkw/3d45a060d77abb8ec125 to your computer and use it in GitHub Desktop.
Save wkw/3d45a060d77abb8ec125 to your computer and use it in GitHub Desktop.
Synchronize Wordpress Custom Meta Fields for Custom Field Suite. This will not work if your field group(s) use loop or relationship fields.
// source: https://uproot.us/forums/questions/1511/importing-data
/*
* ============================== EXPERIMENTAL CUSTOM FIELDS SUITE FIELD SYNC ============================
*
* Importing posts with custom fields even when their names match those in CFS groups. still doesn't make
* them visible to CFS. The map_fields_values below is from a forum post on uproot's site:
* https://uproot.us/forums/questions/1511/importing-data
*
* I have hard-coded the group ID for staff_bios into _sync_cfs_fields(). Call this function after
* import.
* =======================================================================================================
*/
function _sync_cfs_fields(){
$staff_bios_id = 85;
$options['field_groups'] = array($staff_bios_id); // Group ID's that needs to be synced
$result = _map_values_updated($options);
echo $result;die();
}
//
// To perform the sync, uncomment the next line and reload site a single time. then re-comment it.
//
//_sync_cfs_fields();
//
function _map_values_updated($options)
{
global $wpdb;
if (isset($options['field_groups']))
{
$group_ids = (array) $options['field_groups'];
foreach ($group_ids as $group_id)
{
$rules = get_post_meta($group_id, 'cfs_rules', true);
$post_types = $post_ids = $term_ids = '';
$fields = array();
// Get this group's fields
$results = get_post_meta($group_id, 'cfs_fields', true);
foreach ($results as $result)
{
$fields[$result['name']] = $result['id'];
}
if (isset($rules['post_types']))
{
$post_types = implode("','", $rules['post_types']['values']);
$operator = ('==' == $rules['post_types']['operator']) ? 'IN' : 'NOT IN';
$post_types = " AND p.post_type $operator ('$post_types')";
}
if (isset($rules['post_ids']))
{
$post_ids = implode(',', $rules['post_ids']['values']);
$operator = ('==' == $rules['post_ids']['operator']) ? 'IN' : 'NOT IN';
$post_ids = " AND p.ID $operator ($post_ids)";
}
if (isset($rules['term_ids']))
{
$term_ids = implode(',', $rules['term_ids']['values']);
$operator = ('==' == $rules['term_ids']['operator']) ? 'IN' : 'NOT IN';
$term_ids = "
INNER JOIN $wpdb->term_relationships tr ON tr.object_id = p.ID
INNER JOIN $wpdb->term_taxonomy tt ON tt.term_taxonomy_id = tr.term_taxonomy_id AND tt.term_id $operator ($term_ids)";
}
$sql = "
SELECT m.meta_id, m.post_id, m.meta_key
FROM $wpdb->postmeta m
INNER JOIN $wpdb->posts p ON p.ID = m.post_id $post_types $post_ids $term_ids
LEFT JOIN {$wpdb->prefix}cfs_values v ON v.meta_id = m.meta_id
WHERE v.meta_id IS NULL";
$results = $wpdb->get_results($sql);
$tuples = array();
foreach ($results as $result)
{
if (isset($fields[$result->meta_key]))
{
$field_id = $fields[$result->meta_key];
$tuples[] = "($field_id, $result->meta_id, $result->post_id, 0, 0)";
}
}
if (0 < count($tuples))
{
$wpdb->query("INSERT INTO {$wpdb->prefix}cfs_values (field_id, meta_id, post_id, weight, sub_weight) VALUES " . implode(',', $tuples));
}
}
return 'Sync successful';
}
else
{
return '<div>' . __('No field groups selected', 'cfs') . '</div>';
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment