Looping through custom post type in PHP/WordPress
one text
very new to PHP - custom post type, which I'm retrieving and looping through the results to display on the screen. Is this the best way to do it?
Or is there a way to retrieve results from the meta data without having to do:
get_the_ID(),'measure_description'
...for every field in the meta/custom post?
Also, $postauthor = get_post_meta( get_the_ID(),'post_author',true);
shows blank - how can I retrieve the post author from the query/results I have?
$query_args = array(
'post_type' => 'tftracker',
// 'author' => '1',
// 'author_name' => 'Mark',
'order' => 'ASC',
'orderby' => 'meta_value',
'meta_key' => 'result_date',
'nopaging' => true,
'meta_query' => array(
array(
'key' => 'unit_type',
'value' => 'steps',
'compare' => '=',
)
)
);
// Run the query.
$query = new WP_Query( $query_args );
if ( $query->have_posts() ) {
while ( $query->have_posts() ) {
$query->the_post();
$measure = get_post_meta( get_the_ID(),'measure_description',true);
$record = get_post_meta( get_the_ID(),'unit_type',true);
$latestres = get_post_meta( get_the_ID(),'latest_result',true);
$datet = get_post_meta( get_the_ID(),'result_date',true);
$postauthor =get_post_meta( get_the_ID(),'post_author',true);
echo('<ul>');
echo('<li><strong>post_author:</strong> ' . $postauthor . '</li>');
echo('<li><strong>Measure:</strong> ' . $measure . '</li>');
echo('<li><strong>Record:</strong> ' . $record . '</li>');
echo('<li><strong>Result:</strong> ' . $latestres . '</li>');
echo('<li><strong>Date:</strong> ' . $datet . '</li>');
echo('</ul>');
}
} else {
// Display a no posts found message here.
}
// Reset post data.
wp_reset_postdata();
Source