Last active
October 27, 2022 21:51
-
-
Save tevashov/5172454 to your computer and use it in GitHub Desktop.
Get posts from custom tax grouped by terms (MySQL) #WP
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
| // get a list of categories, in this case your custom taxonomy (your_taxonomy_name) | |
| $querystr = "SELECT terms.* FROM $wpdb->term_taxonomy tax LEFT JOIN $wpdb->terms terms ON tax.term_id = terms.term_id WHERE tax.taxonomy = 'retouch_types'"; | |
| $categories = $wpdb->get_results($querystr, OBJECT); | |
| // begin a loop through those terms (categories in your custom taxonomy) | |
| foreach( $categories as $category ) { | |
| echo '<div class="category-header"><h2>'.$category->name.'</h2>'; // print the cat title | |
| echo '<p class="category-description">'.strip_tags(term_description($category->term_id,'retouch_types')).'</p></div>'; // cat description | |
| //select posts in this category, and of a specified content type | |
| $posts = get_posts( array( | |
| 'post_type' => 'retouch', | |
| 'tax_query' => array( | |
| array( | |
| 'taxonomy' => 'retouch_types', | |
| 'field' => 'slug', | |
| 'terms' => $category->slug | |
| ) | |
| ) | |
| )); | |
| // begin cycle through posts of this category | |
| foreach($posts as $post) { | |
| setup_postdata($post); //set up post data for use in the loop (enables the_title(), etc without specifying a post ID) | |
| the_title(); | |
| the_excerpt(); | |
| } // endforeach; | |
| } //endforeach; ?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment