It is necessary to obtain a list of all categories from a custom type of records, each of which will contain a list of records belonging to it. Like this:

ΠšΠ°Ρ‚Π΅Π³ΠΎΡ€ΠΈΡ А Π—Π°ΠΏΠΈΡΡŒ ΠΊΠ°Ρ‚ А 1 Π—Π°ΠΏΠΈΡΡŒ ΠΊΠ°Ρ‚ А 2 Π—Π°ΠΏΠΈΡΡŒ ΠΊΠ°Ρ‚ А N ... ΠšΠ°Ρ‚Π΅Π³ΠΎΡ€ΠΈΡ B ... 

and so on.

In the template I try to combine the functions get_categories() and get_posts() :

 $args = array( 'type' => 'glossary', 'child_of' => 0, 'parent' => '', 'orderby' => 'name', 'order' => 'ASC', 'hide_empty' => 0, 'hierarchical' => 1, 'exclude' => '', 'include' => '', 'number' => 0, 'taxonomy' => 'gl_section', 'pad_counts' => false ); $categories = get_categories( $args ); if( $categories ){ foreach( $categories as $cat ){ // Π”Π°Π½Π½Ρ‹Π΅ Π² ΠΎΠ±ΡŠΠ΅ΠΊΡ‚Π΅ $cat $id = $cat->term_id; $title = $cat->name; echo $id; echo $title; $args = array('category' => $id, 'post_type' => 'glossary' , 'category_name' => $title); print_r( get_posts($args) ); } } 

Result:

  70 D Array ( ) 69 F Array ( ) 71 G Array ( ) 

That is, the category ID and title are displayed, but no posts. I tried to look for a function that displays everything at once - I did not find it. What other ways can there be? Or in WP, in principle, it is impossible to combine records and taxonomy?

Another problem has also come to light: even if I assign a 'category' => ΠΊΠΎΠ½ΠΊΡ€Π΅Ρ‚Π½ΠΎΠ΅ числовоС Π·Π½Π°Ρ‡Π΅Π½ΠΈΠ΅ parameter in the $args array, the effect is the same, an empty array is output.

    1 answer 1

    Good day!

    You did everything right, but the error is that no value is assigned to the $ post variable, so the array of posts is empty.

    First you need to set global post data (out of loop) - setup_postdata () .

    Do not forget to reset the global value override via wp_reset_postdata () .

    Below is the code (real worker, done for your project), with the support of child categories. If there are strange moments - ready to clarify, ask a question.

     <?php //Запрос ΠΊΠ°Ρ‚Π΅Π³ΠΎΡ€ΠΈΠΉ $args = array( 'parent'=>'0', 'pad_counts'=>true, ); $categories = get_categories($args); //ΠŸΠ΅Ρ€Π΅Π±ΠΎΡ€ ΠΊΠ°Ρ‚Π΅Π³ΠΎΡ€ΠΈΠΉ foreach ($categories as $category) { if($category->parent == 0) { //Π’Ρ‹Π²ΠΎΠ΄ΠΈΠΌ ΠΈΠ½Ρ„ΠΎΡ€ΠΌΠ°Ρ†ΠΈΡŽ ΠΎ ΠΊΠ°Ρ‚Π΅Π³ΠΎΡ€ΠΈΠΈ $output .= $category->category_nicename; $output .= $category->cat_name; echo $output; //Запрос Π΄ΠΎΡ‡Π΅Ρ€Π½ΠΈΡ… ΠΊΠ°Ρ‚Π΅Π³ΠΎΡ€ΠΈΠΉ (ΠΏΠΎΠ΄ΠΊΠ°Ρ‚Π΅Π³ΠΎΡ€ΠΈΠΉ) $argsChild = array( 'child_of'=>$category->term_id, ); $childCategories = get_categories($argsChild); //ΠŸΠ΅Ρ€Π΅Π±ΠΎΡ€ ΠΏΠΎΠ΄ΠΊΠ°Ρ‚Π΅Π³ΠΎΡ€ΠΈΠΉ foreach ($childCategories as $childCategory) { //Π’Ρ‹Π²ΠΎΠ΄ΠΈΠΌ ΠΈΠ½Ρ„ΠΎΡ€ΠΌΠ°Ρ†ΠΈΡŽ ΠΎ ΠΏΠΎΠ΄ΠΊΠ°Ρ‚Π΅Π³ΠΎΡ€ΠΈΠΈ $output .= $childCategory->category_nicename; $output .= $childCategory->cat_name; echo $output; //ΠŸΠΎΠ»ΡƒΡ‡Π°Π΅ΠΌ список постов ΠΏΠΎΠ΄ΠΊΠ°Ρ‚Π΅Π³ΠΎΡ€ΠΈΠΈ $args = array( 'category' => $childCategory->term_id, ); $categoryPosts = get_posts( $args ); //Π’Ρ‹Π²ΠΎΠ΄ΠΈΠΌ посты echo '<ul>'; foreach( $categoryPosts as $post ) { setup_postdata($post); ?> <li><?php the_title(); ?></li> <?php } wp_reset_postdata(); echo '</ul>'; } } } ?>