Created
November 2, 2018 00:45
-
-
Save tomatillodesign/3804819dd076d50a3eda3595dd265f70 to your computer and use it in GitHub Desktop.
Sample WP-Query
This file contains 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 | |
// Reference: https://codex.wordpress.org/Class_Reference/WP_Query | |
$args = array( | |
'post_type' => 'resource', // enter your custom post type or use 'post' for regular posts | |
'tax_query' => array( // see https://codex.wordpress.org/Class_Reference/WP_Query#Taxonomy_Parameters | |
array( | |
'taxonomy' => 'people', | |
'field' => 'slug', | |
'terms' => 'bob', | |
), | |
), | |
'orderby' => 'date', | |
'order' => 'ASC', | |
'post_status' => 'publish', | |
'posts_per_page'=> -1, | |
); | |
// The Query | |
$the_query = new WP_Query( $args ); | |
// The Loop; see https://developer.wordpress.org/themes/basics/the-loop/ | |
if ( $the_query->have_posts() ) { | |
while ( $the_query->have_posts() ) { | |
$the_query->the_post(); | |
//vars, lots of WP functions available to get info from this specific post | |
$title = get_the_title(); | |
$date = get_the_date(); | |
$permalink = get_the_permalink(); | |
//Do something here | |
echo '<a href="' . $permalink . '">' . $title . ' (' . $date . ')</a>'; | |
} | |
} | |
wp_reset_postdata(); // must reset data for rest of page to work, see https://developer.wordpress.org/reference/functions/wp_reset_postdata/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
add_action('genesis_entry_content', 'your_function_name_here', 14);
function your_function_name_here() {
// Sample query goes here
}