Hi all,
I'm working on a new plugin but i came across a very frustrating issue.
I register several metaboxes and in a few of them i use a query. Now to get my metavalue for the other metaboxes i use get_post_meta($post->ID,'_social',TRUE); However the metabox with the query in it somehow resets the $post variable. And i can't reset the query with wp_reset_query(). I've tried all kinds of querying and unset methods. But none of them seem to work.
My file structure is like this:
- wp-stagemanager.php
- admin(folder)
- artists.php
- events.php
- metaboxes.php
- assets(folder)
- css(folder)
- img(folder)
- js(folder)
- cache(folder)
- data.json
- front(folder)
- functions.php
in wp-stagemanager.php i include the admin/metaboxes.php which has the following code...
<?php
add_action('admin_init', 'WPSM_add_metabox', 1);
add_action('save_post', 'WPSM_save_metabox');
function WPSM_add_metabox() {
add_meta_box( 'WPSM_meta_social', __( 'Social Media', 'wp-stagemanager_textdomain' ),
'WPSM_init_meta_social', 'artist' );
add_meta_box( 'WPSM_meta_datetime', __( 'Event Date & Time', 'wp-stagemanager_textdomain' ),
'WPSM_init_meta_datetime', 'event');
add_meta_box( 'WPSM_meta_tickets', __( 'Event Tickets', 'wp-stagemanager_textdomain' ),
'WPSM_init_meta_tickets', 'event' );
add_meta_box( 'WPSM_meta_artists', __( 'Event Artists', 'wp-stagemanager_textdomain' ),
'WPSM_init_meta_artists', 'event' );
}
function WPSM_init_meta_social(){
global $post;
$social = get_post_meta($post->ID,'_social',TRUE);
$socialnonce = wp_create_nonce(__FILE__);
?>
<div class="social_meta_control">
<table width="100%">
<tr>
<td>
<label for="web" >Website</label>
<input type="text" name="_social[web]" id="web" value="<?php if(!empty($social['web'])) echo $social['web']; ?>"/>
</td>
<td>
<label for="ms">MySpace</label>
<input type="text" name="_social[mysp]" id="ms" value="<?php if(!empty($social['mysp'])) echo $social['mysp']; ?>"/>
</td>
<td>
<label for="yt">YouTube</label>
<input type="text" name="_social[yout]" id="yt" value="<?php if(!empty($social['yout'])) echo $social['yout']; ?>"/>
</td>
</tr>
<tr>
<td>
<label for="fb">Facebook</label>
<input type="text" name="_social[face]" id="fb" value="<?php if(!empty($social['face'])) echo $social['face']; ?>"/>
</td>
<td>
<label for="sp">Spotify</label>
<input type="text" name="_social[spot]" id="sp" value="<?php if(!empty($social['spot'])) echo $social['spot']; ?>"/>
</td>
<td>
<label for="la">last.fm</label>
<input type="text" name="_social[last]" id="la" value="<?php if(!empty($social['last'])) echo $social['last']; ?>"/>
</td>
</tr>
</table>
<p class="description">Insert the full url to artist website or social media </p>
</div>
<input type="hidden" name="social_nonce" value="<?php echo $socialnonce ?>" />
<?php
}
function WPSM_init_meta_datetime(){
global $post;
$startdate = get_post_meta($post->ID,'_startdate',TRUE);
$startdatenonce = wp_create_nonce(__FILE__);
$enddate = get_post_meta($post->ID,'_enddate',TRUE);
$enddatenonce = wp_create_nonce (__FILE__);
$time = get_post_meta($post->ID,'_time',TRUE);
$timenonce = wp_create_nonce (__FILE__);
?>
<div class="date_meta_control">
<script>
jQuery(function() {
var dates = jQuery( "#from, #to" ).datepicker({
showButtonPanel: true,
onSelect: function( selectedDate ) {
var option = this.id == "from" ? "minDate" : "maxDate",
instance = jQuery( this ).data( "datepicker" );
date = jQuery.datepicker.parseDate(
instance.settings.dateFormat ||
jQuery.datepicker._defaults.dateFormat,
selectedDate, instance.settings );
dates.not( this ).datepicker( "option", option, date );
dates.datepicker( "option", "dateFormat", 'yy-mm-dd' );
}
});
jQuery('#time').timeEntry({show24Hours: true});
});
</script>
<table width="100%">
<tr>
<td>
<label for="from">Start date</label>
<input type="text" id="from" name="_startdate" value="<?php if(!empty($startdate)) echo $startdate; ?>"/>
</td>
<td>
<label for="to">End date</label>
<input type="text" id="to" name="_enddate" value="<?php if(!empty($enddate)) echo $enddate; ?>" />
</td>
<td>
<label for="time">Time</label>
<input type="text" id="time" name="_time" value="<?php if(!empty($time)) echo $time; ?>" />
</td>
</tr>
</table>
</div>
<input type="hidden" name="startdate_nonce" value="<?php echo $startdatenonce ?>" />
<input type="hidden" name="enddate_nonce" value="<?php echo $enddatenonce ?>" />
<input type="hidden" name="time_nonce" value="<?php echo $timenonce ?>" />
<?php
}
function WPSM_init_meta_tickets(){
global $post;
$tickets = get_post_meta($post->ID,'_tickets',TRUE);
$ticketsnonce = wp_create_nonce (__FILE__);
?>
<div class="ticket_meta_control">
<table>
<tr>
<td>
<label for="tlink" >Ticket Link</label>
<input type="text" name="_tickets[link]" id="tlink" value="<?php if(!empty($tickets['link'])) echo $tickets['link']; ?>"/>
</td>
<td>
<label for="tprice">Price</label>
<input type="text" name="_tickets[price]" id="tprice" value="<?php if(!empty($tickets['price'])) echo $tickets['price']; ?>"/>
</td>
</tr>
</table>
</div>
<input type="hidden" name="tickets_nonce" value="<?php echo $ticketsnonce ?>" />
<?php
}
function WPSM_init_meta_artists(){
global $post;
$artists = get_post_meta($post->ID,'_artists',TRUE);
$artistsnonce = wp_create_nonce (__FILE__);
?>
<div class="artists_meta_control">
<table width="100%">
<tr>
<td>
<div class="form_artists">
<input type="text" name="_artists" value="" id="fas" />
</div>
<script type="text/javascript" charset="utf-8">
jQuery(function($){
var t4 = new $.TextboxList('#fas', {unique: true, plugins: {autocomplete: {}}});
<?php //WPSM_artists_selected($artists); ?>
t4.getContainer().addClass('textboxlist-loading');
$.ajax({url: '<?php WPSM_artists_autocomplete(); ?>', dataType: 'json', success: function(r){
t4.plugins['autocomplete'].setValues(r);
t4.getContainer().removeClass('textboxlist-loading');
}});
});
</script>
</td>
</tr>
</table>
</div>
<input type="hidden" name="artists_nonce" value="<?php echo $artistsnonce ?>" />
<?php
query_posts($query_string);
}
function WPSM_artists_autocomplete(){
global $post;
global $wpdb;
global $wp_query;
$response = array();
$artists = $wpdb->get_results("
SELECT *
FROM $wpdb->posts AS <code>post</code>
WHERE post.post_status = 'publish'
AND post.post_type = 'artist'
");
if($artists){
foreach($artists as $poster){
setup_postdata($poster);
$image_id = get_post_thumbnail_id();
$image_url = wp_get_attachment_image_src($image_id,'artist-icon');
$image_url = $image_url[0];
$response[] = array( get_the_ID() , get_the_title() , null, '<img src="'.$image_url.'" />'. get_the_title());
}
}
wp_reset_query();
$output = json_encode($response);
$data = WPSM_CACHE_DIR."/data.json";
$fh = fopen($data, 'w') or die("can't open file");
fwrite($fh, $output);
fclose($fh);
echo WPSM_CACHE_URL."/data.json";
}
function WPSM_artists_selected($arr){
global $wp_query;
$wp_query = new WP_Query( array( 'post_type' => 'artist', 'posts_per_page' => -1 ) );
while ( $wp_query->have_posts() ) : $wp_query->the_post();
if(in_array(get_the_ID(),$arr)){
echo "t4.add('".get_the_title()."','".get_the_ID()."');";
}
endwhile;
wp_reset_query();
}
function WPSM_save_metabox($post_id){
$boxes = array('_startdate','_enddate','_time','_tickets','_artists');
foreach($boxes as $box){
// Strip underscore
$box_name = str_replace('_','',$box);
// Check metabox
if (!wp_verify_nonce($_POST[$box_name.'_nonce'],__FILE__)) return $post_id;
// Check data
$current_data = get_post_meta($post_id, $box, TRUE);
$new_data = $_POST[$box];
// Remove Duplicates
if($box == '_artists'){
$arr = explode(',',$new_data);
$unique = array_unique($arr);
$new_data = $unique;
}
WPSM_clean_metabox($box);
// Save or update
if ($current_data) {
if (is_null($new_data)) delete_post_meta($post_id,$box);
else update_post_meta($post_id,$box,$new_data);
}elseif (!is_null($new_data)){
add_post_meta($post_id,$box,$new_data,TRUE);
}
}
return $post_id;
}
function WPSM_clean_metabox(&$arr){
if (is_array($arr)){
foreach ($arr as $i => $v){
if (is_array($arr[$i])) {
events_metabox_clean($arr[$i]);
if (!count($arr[$i])) {
unset($arr[$i]);
}
}else {
if (trim($arr[$i]) == '') {
unset($arr[$i]);
}
}
}
if (!count($arr)) {
$arr = NULL;
}
}
}
?>
The queries that screw things up are within the
WPSM_artists_autocomplete() and WPSM_artists_selected() functions.
WPSM_artists_autocomplete
function WPSM_artists_autocomplete(){
global $post;
global $wpdb;
global $wp_query;
$response = array();
$artists = $wpdb->get_results("
SELECT *
FROM $wpdb->posts AS <code>post</code>
WHERE post.post_status = 'publish'
AND post.post_type = 'artist'
");
if($artists){
foreach($artists as $poster){
setup_postdata($poster);
$image_id = get_post_thumbnail_id();
$image_url = wp_get_attachment_image_src($image_id,'artist-icon');
$image_url = $image_url[0];
$response[] = array( get_the_ID() , get_the_title() , null, '<img src="'.$image_url.'" />'. get_the_title());
}
}
wp_reset_query();
$output = json_encode($response);
$data = WPSM_CACHE_DIR."/data.json";
$fh = fopen($data, 'w') or die("can't open file");
fwrite($fh, $output);
fclose($fh);
echo WPSM_CACHE_URL."/data.json";
}
WPSM_artists_selected
function WPSM_artists_selected($arr){
global $wp_query;
$wp_query = new WP_Query( array( 'post_type' => 'artist', 'posts_per_page' => -1 ) );
while ( $wp_query->have_posts() ) : $wp_query->the_post();
if(in_array(get_the_ID(),$arr)){
echo "t4.add('".get_the_title()."','".get_the_ID()."');";
}
endwhile;
wp_reset_query();
}
Is this a scope issue? When i use a function_exsists on the wp_reset_query() it turns out true.
I hope you guys can help cause i'm really stuck here.
Thanks!
Cheers,
R.