Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save BenBroide/59ff7b0c304c09e4fb3a6c96f27e8985 to your computer and use it in GitHub Desktop.

Select an option

Save BenBroide/59ff7b0c304c09e4fb3a6c96f27e8985 to your computer and use it in GitHub Desktop.
<?php
//----------------------------
// Old way of getting post and custom fields - Get fields inside the loop with get_post_meta() that has 2-3 parameters
//----------------------------
$the_query = new WP_Query( $args );
// Loop
if ( $the_query->have_posts() ) {
while ( $the_query->have_posts() ) {
$the_query->the_post();
echo get_the_title();
echo get_post_meta($post_id, 'location' );
echo get_post_meta($post_id, 'date' );
}
}
//----------------------------
// New way - Set up custom fields in the main query and print them in the loop from the object by fields name.
//----------------------------
// Get all custom fields
$the_query = new WP_Query( 'post_fields' = TRUE );
// Get specific custom fields
$the_query = new WP_Query( 'post_fields' = array( 'location' , 'date' ) );
// Loop
if ( $the_query->have_posts() ) {
while ( $the_query->have_posts() ) {
$the_query->the_post();
echo get_the_title();
echo $the_query->post_field( 'location' );
echo $the_query->post_field( 'date' );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment