Created
July 13, 2021 19:46
-
-
Save jorpdesigns/561f1292a6fe9c98d97febc47d7c3f3b to your computer and use it in GitHub Desktop.
Function for querying custom post type based on custom taxonomy
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 custom_post_tax_query() { | |
$args = array( | |
'post_type' => 'custom-post-type', // Replace with custom post type | |
'posts_per_page' => -1, | |
'order' => 'ASC', | |
'orderby' => 'menu_order', | |
'tax_query' => array( array( | |
'taxonomy' => 'custom_taxonomy', // Replace with custom taxonomy | |
'field' => 'slug', | |
'terms' => array( sanitize_title( 'Custom Taxonomy Term' ) ) // Replace with custom taxonomy term | |
) ) | |
); | |
$posts = get_posts($args); | |
$output = ''; | |
if ( $posts ) { | |
foreach ($posts as $post) { | |
$output .= '<li><a href="'. get_the_permalink() .'">'. get_the_title() .'</a></li>'; | |
} | |
wp_reset_postdata(); | |
} else { | |
$output .= '<li>Coming Soon</li>'; | |
} | |
return '<div class="custom-post-loop"><ul>'. $output .'</ul></div>'; | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment