Why not output the entries from a specific category Wordpress?

He created his own record type:
add_action('init', 'create_post_type');
function create_post_type()
{
register_post_type(
'courses',
array(
 'labels' => array(
 'name' => 'training Program',
 'singular_name' => 'education Program',
 'add_new' => 'Add course',
 'add_new_item' => 'Add program',
 'edit_item' => 'Edit product',
 'new_item' => 'New program',
 'view_item' => 'Watch the program',
 'search_items' => 'Search program',
 'not_found' => 'Not found',
 'not_found_in_trash' => 'Not found in trash',
 'parent_item_colon' => ",
 'menu_name' => 'Courses',
),
 'public' => true,
 'has_archive' => true,
 'menu_position' => 4,
 'menu_icon' => 'dashicons-welcome-learn-more',
 'supports' => array(
'title',
'revisions',
'page-attributes',
'thumbnail'
),
)
);
}

and registered the Dachshund:
add_action('init', 'create_course_taxonomies');
create_course_taxonomies function()
{
 register_taxonomy('type_course', array('courses'), array(
 'hierarchical' => true,
 'show_ui' => true,
 'query_var' => true,
 'show_admin_column' => true,
));
}

Added in the admin record, separated them by category.
Trying to record to their lead, but somehow, the output is an empty array.
Get this:
$courses_cats = get_terms('type_course');
foreach ($courses_cats as $courses_cat) {
 $courses = get_posts(array(
 'numberposts' => -1,
 'category' => $courses_cat->term_id,
 'orderby' => 'date',
 'order' => 'DESC',
 'post_type' => 'courses',
 'suppress_filters' => true,
));
}
March 23rd 20 at 18:42
1 answer
March 23rd 20 at 18:44
Solution
The problem is the arguments to the function get_posts. Instead of the category you need to use the tax_query.

For example:

$courses = get_posts(array(
 'numberposts' => -1,
 'tax_query' => array( array (
 'taxonomy' => 'type_course',
 'field' => 'term_id',
 'terms' => $courses_cat->term_id
 ) )
 'orderby' => 'date',
 'order' => 'DESC',
 'post_type' => 'courses',
 'suppress_filters' => true,
));
thank you! - aida_Gutkowski45 commented on March 23rd 20 at 18:47

Find more questions by tags WordPress