Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Redirect Problem with WP_Query in an admin function WordPress


I am putting together a couple of metaboxes for a custom post type in Wordpress 3.0. Got the code working spot on in the admin panel, everything displays, saves and loads as it should but as soon as I load the front end I get this error from FF & Safari:

The page isn't redirecting properly:
Firefox has detected that the server is redirecting the request for this address in a way that will never complete.</blockquote>

I have boiled it down to being caused by one function, which I am calling in order create an array of post_ID -> post_Title, to later use to populate a select box. Taking out the calls to the function fix everything, but I can't fault the function.

function mf_get_custom_post_list($type){ $post_list = array(); $meta_query = new WP_Query('post_type='.$type); if ( $meta_query->have_posts() ) : while ( $meta_query->have_posts() ) : $meta_query->the_post(); $post_list[get_the_ID()] = get_the_title(); endwhile; endif; //Reset Query wp_reset_query(); return $post_list; }


Answers (1)


Utkarsh Kukreti answers:

Can you post the rest of the code?

John Farrow comments:

this is the script for the meta boxes

*Meta Boxes


function mf_SALF_get_custom_post_list($type){
$post_list = array();
$meta_query = new WP_Query('post_type='.$type);

if ($meta_query->have_posts()):while($meta_query->have_posts()):$meta_query->the_post();
$post_list[get_the_ID()] = get_the_title();

return $post_list;

$prefix = 'mf_SALF_meta_';

$meta_box = array(
'id' => 'videos',
'title' => 'date',
'page' => 'Program',
'context' => 'normal',
'priority' => 'high',
'fields' => array(

'name' => 'Venue',
'id' => $prefix . 'venue',
'type' => 'select2',
'options' => mf_SALF_get_custom_post_list('Venues')
'name' => 'Artist',
'id' => $prefix . 'artist',
'type' => 'select2',
'options' => mf_SALF_get_custom_post_list('Artists')


// Add meta box
function mf_SALF_meta_box(){
global $meta_box;

add_meta_box($meta_box['id'], $meta_box['title'], 'mf_SALF_meta_show_box', $meta_box['page'], $meta_box['context'], $meta_box['priority']);
// Callback function to show fields in meta box
function mf_SALF_meta_show_box() {
global $meta_box, $post;

// Use nonce for verification
echo '<input type="hidden" name="mytheme_meta_box_nonce" value="', wp_create_nonce(basename(__FILE__)), '" />';

echo '<table class="form-table">';

foreach ($meta_box['fields'] as $field) {
// get current post meta data
$meta = get_post_meta($post->ID, $field['id'], true);

echo '<tr>',
'<th style="width:20%"><label for="', $field['id'], '">', $field['name'], '</label></th>',
switch ($field['type']) {

case 'select2':

echo '<select name="', $field['id'], '" id="', $field['id'], '">';
foreach ($field['options'] as $ID => $option) {
echo '<option value="'. $ID .'" ', $meta == $ID ? ' selected="selected"' : '', '>', $option, '</option>';
echo '</select>';

echo '<td>',

echo '</table>';
add_action('save_post', 'mf_SALF_meta_save_data');

// Save data from meta box
function mf_SALF_meta_save_data($post_id) {
global $meta_box;

// verify nonce
if (!wp_verify_nonce($_POST['mytheme_meta_box_nonce'], basename(__FILE__))) {
return $post_id;

// check autosave
return $post_id;

// check permissions
if ('page' == $_POST['post_type']) {
if (!current_user_can('edit_page', $post_id)) {
return $post_id;
} elseif (!current_user_can('edit_post', $post_id)) {
return $post_id;

foreach ($meta_box['fields'] as $field) {
$old = get_post_meta($post_id, $field['id'], true);
$new = $_POST[$field['id']];

if ($new && $new != $old) {
update_post_meta($post_id, $field['id'], $new);
} elseif ('' == $new && $old) {
delete_post_meta($post_id, $field['id'], $old);


this is being called by include('meta.php'); in functions.php

I am sure it is that function at fault, as if I comment out the two calls to it, everything goes back to normal.

John Farrow comments:

The more I experiment, they more I think the solution is to obtain the ID's and titles without using The Loop

John Farrow comments:

But I don't know how!!

Utkarsh Kukreti comments:

Could you enable this and link me to the site? (or PM)

Need to have a look at the redirect.

John Farrow comments:

unfortunately not, too much sensitive client stuff.

I have now fixed it, so don't know where I stand here!!