Skip to content

Instantly share code, notes, and snippets.

@jorpdesigns
Created July 13, 2021 19:46
Show Gist options
  • Save jorpdesigns/561f1292a6fe9c98d97febc47d7c3f3b to your computer and use it in GitHub Desktop.
Save jorpdesigns/561f1292a6fe9c98d97febc47d7c3f3b to your computer and use it in GitHub Desktop.
Function for querying custom post type based on custom taxonomy
<?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