Last active
August 14, 2016 01:22
-
-
Save BenBroide/59ff7b0c304c09e4fb3a6c96f27e8985 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?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