Created
August 11, 2017 13:57
-
-
Save mohsinr/8c6fbee3e93623d51536e90bcfd63135 to your computer and use it in GitHub Desktop.
Assign tags to all posts in a custom post type, one time , quick and dirty code
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
# One time, assign missing tags | |
function assign_tag($id){ | |
$row = unserialize(get_post_meta($id,'_nhome_meta',true)); | |
$ownership = $row['OWNERSHIP']; | |
$tag = ''; | |
if (strpos($ownership, 'For profit') !== false) { | |
$tag = 'For profit Nursing Homes'; | |
}elseif(strpos($ownership, 'Government') !== false) { | |
$tag = 'Government Nursing Homes'; | |
}elseif(strpos($ownership, 'Non profit') !== false) { | |
$tag = 'Non profit Nursing Homes'; | |
} | |
var_dump($id); | |
// var_dump($tag); exit; | |
if($tag){ wp_set_post_tags( $id, $tag , true ); } | |
} | |
/* The 2nd Query (without global var) */ | |
$args = array( | |
'post_type' => 'nhome', | |
'nopaging' => true, | |
'posts_per_page' => -1, | |
'fields' => 'ids', | |
); | |
$ids = new WP_Query( $args ); | |
$run = $_GET['run']; | |
if($run == 'yes'){ | |
foreach ($ids->posts as $key => $post_id) { | |
assign_tag($post_id); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment