I am working on a project where I have a custom post type ‘companies’ and on this edit page in the WordPress admin I have some custom made meta boxes, for saving company information, opening hours, pictures etc etc.
Since yesterday I added a meta box for Leads to show the posts in the custom post type “leads” of this companies. To show these posts I use a new WP_Query. The leads posts are displayed correctly, but the meta data from my opening hours and pictures are gone now.
Does anyone know what I need to change to this loop to use it in a WordPress admin. (I also tried wp_reset_postdata();
at the end op the loop).
I register the meta boxes first off course and these are the functions in it.
// Company Leads
function stn_inner_company_leads( $post ){
$args = array(
'post_type' => 'leads',
'meta_query' => array(
array(
'key' => 'page_id',
'value' => $post->ID,
)
),
);
$leads = new WP_Query( $args );
if( $leads->have_posts() ) {
$counter = 0;
echo '<table id="lead_table" width="100%">';
echo '<tbody>';
while ( $leads->have_posts() ) { $leads->the_post();
$counter++;
$output = '<tr>';
$output .= '<td># ' . $counter . '</td>';
$output .= '<td><a href="' . get_edit_post_link() . '">' . get_the_title() . '</a></td>';
$output .= '<td>' . get_the_date( 'l, F j, Y g:i a' ) . '</td>';
$output .= '</tr>';
echo $output;
}
echo '</tbody>';
echo '</table>';
} else {
_e( 'This company has no leads.' );
}
wp_reset_query();
}
This is my function for opening hours:
// Opening hours
function stn_inner_opening_hours( $post ) {
global $company;
echo '<div class="stn_inner_box">';
echo '<fieldset>';
foreach ( $company['opening_hours'] as $key => $value) {
$value[1] = get_post_meta( $post->ID, $key . '_is_open', true );
$value[2] = get_post_meta( $post->ID, $key . '_open', true );
$value[3] = get_post_meta( $post->ID, $key . '_closed', true );
$output = '<ul class="time_row">';
$output .= '<label for="company_' . $key . '">';
$output .= __( $value[0] ) . ':';
$output .= '</label>';
$output .= '<li><select name="company_' . $key . '_is_open' . '">';
$output .= '<option ' . ( $value[1] == 'open' ? 'selected' : '' ) . ' value="open">' . __( 'Open' ) . '</option>';
$output .= '<option ' . ( $value[1] == 'closed' ? 'selected' : '' ) . ' value="closed">' . __( 'Closed' ) . '</option>';
$output .= '</select></li>';
$output .= '<li>' . __( 'From' ) . ':</li>';
$output .= '<li><select name="company_' . $key . '_open' . '">';
for ( $n = strtotime("00:00"), $e = strtotime("23:30"); $n <= $e; $n += 1800 ) {
$this_time = date("H:i", $n);
$output .= '<option ' . ( $value[2] == $this_time ? 'selected' : '' ) . ' value="' . esc_attr( $this_time ) . '" >' . $this_time . '</option>';
}
$output .= '</select></li>';
$output .= '<li>' . __( 'Till' ) . ':</li>';
$output .= '<li><select name="company_' . $key . '_closed' . '">';
for ( $n = strtotime("00:00"), $e = strtotime("23:30"); $n <= $e; $n += 1800 ) {
$this_time = date("H:i",$n);
$output .= '<option ' . ( $value[3] == $this_time ? 'selected' : '' ) . ' value="' . esc_attr( $this_time ) . '" >' . $this_time . '</option>';
}
$output .= '</select></li>';
$output .= '</ul>';
echo $output;
}
echo '</fieldset>';
echo '</div>';
}
var_dump($post); gives me the following result:
object(WP_Post)#352 (24) {
["ID"]=>
string(5) "44625"
["post_author"]=>
string(1) "1"
["post_date"]=>
string(19) "2014-03-31 06:54:14"
["post_date_gmt"]=>
string(19) "2014-03-31 06:54:14"
["post_content"]=>
string(0) ""
["post_title"]=>
string(12) "Test salon 2"
["post_excerpt"]=>
string(0) ""
["post_status"]=>
string(7) "publish"
["comment_status"]=>
string(4) "open"
["ping_status"]=>
string(4) "open"
["post_password"]=>
string(0) ""
["post_name"]=>
string(12) "test-salon-2"
["to_ping"]=>
string(0) ""
["pinged"]=>
string(0) ""
["post_modified"]=>
string(19) "2014-03-31 06:56:55"
["post_modified_gmt"]=>
string(19) "2014-03-31 06:56:55"
["post_content_filtered"]=>
string(0) ""
["post_parent"]=>
string(1) "0"
["guid"]=>
string(59) "http://www.mywebsite.com/?post_type=companies&p=44625"
["menu_order"]=>
string(1) "0"
["post_type"]=>
string(9) "companies"
["post_mime_type"]=>
string(0) ""
["comment_count"]=>
string(1) "0"
["filter"]=>
string(4) "edit"
}