You have double posted this question, and in your comment in the previous question you said that you don’t know if you are using build-in categories or custom taxonomies.
Essentially, built-in categories and custom taxonomies are both Taxonomies, and the “categories” that you create in the back end are in actual fact just terms of the taxonomy ‘category’, so essentially, your query will be exactly the same for both build-in categories and custom taxonomies
If you need to know if you are dealing with categories or custom taxonomies within a post type, you can use get_object_taxonomies
to retrieve these information. This function will also be used later to retrieve a list of categories or custom taxonomies to compile your custom query. Just change $post_type
to the actual name of your post type, in your case faq
$taxonomy_names = get_object_taxonomies( '$post_type' );
print_r( $taxonomy_names);
If you are using custom taxonomies, the following will be returned
Array ( [0] => TAXONOMY_NAME )
If you are using categories, the following will be returned
Array
(
[0] => category
[1] => post_tag
[2] => post_format
)
Ok, done with the nitty gritty stuff. Lets get to the query. You are almost there. The correct way here is to use the taxonomy parameters within WP_Query
to construct your query.
$args = array(
'post_type' => 'POST TYPE OF WHICH POSTS YOU WANT TO RETRIEVE',
'tax_query' => array(
array(
'taxonomy' => 'NAME OF YOUR TAXONOMY',
'field' => 'WHICH FIELD TO USE TO RETRIEVE TERMS',
'terms' => 'NAME OF YOUR TERM'
)
)
);
$query = new WP_Query( $args );
Another thing of note, caller_get_posts
is longtime depreciated, and was replaced with ignore_sticky_posts
This is how your final query should look like. I just copied, corrected and tested your code as is, so if there are any other modifications that you need to make, feel free and adapt
<?php
$post_type="faq";
$taxonomies = get_object_taxonomies( $post_type );
foreach ($taxonomies as $taxonomy){
$terms = get_terms($taxonomy, array('orderby' => 'id', 'order' => 'ASC', 'exclude' => '1'));
if ( !empty( $terms ) && !is_wp_error( $terms ) ){
foreach ( $terms as $term ) {
$args = array(
'post_type' => $post_type,
'orderby' => 'title',
'order' => 'ASC',
'ignore_sticky_posts' => 1,
'post_status' => 'publish',
'posts_per_page' => - 1,
'tax_query' => array(
array(
'taxonomy' => $taxonomy,
'field' => 'slug',
'terms' => $term->slug
)
)
);
$my_query = null;
$my_query = new WP_Query($args);
if ($my_query->have_posts()) {
echo '<div class="aro"><div class="wrap"><h2>' . $term->name . '</h2></div><div class="details"><div class="wrap">';
while ($my_query->have_posts()) : $my_query->the_post(); ?>
<div class="info">
<h3><?php the_title(); ?></h3>
<?php the_content(); ?>
</div>
<?php
endwhile;
} // END if have_posts loop
echo '</div></div></div>'; // Close 'details', 'wrap', & 'aro' DIVs
wp_reset_query();
} // END foreach $terms
}
}
?>