Last active
December 17, 2015 07:19
-
-
Save johnsparrow/5572286 to your computer and use it in GitHub Desktop.
Custom post type loop with optional extra arguments
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 | |
function do_custom_pt_query( $args = array() ){ | |
global $post; | |
$def_args = array('post_type' => 'on_air_programs', | |
'numberposts' => -1, | |
'posts_per_page' => -1, | |
'orderby' => 'meta_value_num', | |
'order' => 'ASC'); | |
// Merge the defaults with defined parameters. You should be able to override the defaults be defining, say, 'orderby', when you call each function | |
$new_args = array_merge($def_args, $args); | |
$the_query = get_posts( $new_args ) | |
if( is_array($the_query) && ( count($the_query) > 0 ) ){ | |
// Put in your loop container here, if any results | |
} | |
foreach( $the_query as $post ){ | |
setup_postdata( $post ); | |
// Do your loop here, treating it the same as main loop with $post object (i.e. the_title(), etc. should work) | |
} // foreach | |
if( is_array($the_query) && ( count($the_query) > 0 ) ){ | |
// Put in your loop container close tag(s) here, if any results | |
} | |
wp_reset_postdata(); | |
return $the_query; // Just in case you want to use the results | |
} | |
// Then all you have to do is call a function each time...? | |
do_custom_pt_query( array('days' => 'weekdays', 'meta_key' => 'eastern_weekday_times') ); | |
do_custom_pt_query( array('days' => 'saturdays', 'meta_key' => 'saturdays') ); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment