• Resolved combi

    (@combinesom)


    At the begining I would like to greet all the WordPress community and excuse all for my English!

    I made a page using the Custom Post Type. I created categories and sub-categories, and I added to them posts. Now, on one site I want to show it all. The following code throws me everything but not exactly like I wanted. So there is a problem with the overarching categories because the script throws all posts belonging to the sub-category also included in each category parent. If this were the usual entries I could used in_category function. But these are Custom Post Type and this feature does not work. How to replace in_category function in the Custom post types?

    Below my code:

    <?php
    $categories = get_terms('MY_CUSTOM_TAXONOMY',array('parent' => 0 , 'hide_empty'=> '0' ));
    
    foreach ( $categories as $category ) {
    
    if ( $category->parent > 0 ) {
        continue;
    }
    
    $i = 0;
    echo '<h1 style="font-weight:bold">' . $category->name . '</h1>';
    
    $posts = get_posts( array( 'MY_CUSTOM_TAXONOMY' => $category->name, 'post_type' => 'MY_CUSTOM_POST_TYPE' ) );
    echo '<ul>';
    foreach ( $posts as $post ) {
    
        $child_categories = get_term_children( $category->term_id, 'MY_CUSTOM_TAXONOMY' );
    
        if ( $child_categories && in_category( $child_categories, $post->ID ) ) {
            continue;
        }
    
    setup_postdata($post);
    echo '<li>'; the_title();   echo '</li>';
    }
     echo '</ul>';
    
    $categories2 = get_terms('MY_CUSTOM_TAXONOMY',array('parent' => $category->term_id , 'hide_empty'=> '0' ));
    
    foreach ( $categories2 as $category ) {
    
        $j = 0;
        echo '<h2>' . $category->name . '</h2>';
    
    $posts = get_posts( array( 'MY_CUSTOM_TAXONOMY' => $category->name, 'post_type' => 'MY_CUSTOM_POST_TYPE' ) );  
    
    echo '<ul>';
    foreach($posts as $post) :
    setup_postdata($post);  
    
    echo '<li>'; the_title();   echo '</li>';
    								endforeach;
    echo '</ul>';
    
    }
    }
    ?>
Viewing 1 replies (of 1 total)
  • Thread Starter combi

    (@combinesom)

    ok, i found solution:

    <?php
    
    $args=array(
    'post_type'                => 'biblioteka',
    'child_of'                 => 0,
    'parent'                   => '',
    'orderby'                  => 'name',
    'order'                    => 'ASC',
    'hide_empty'               => 1,
    'hierarchical'             => 1,
    'exclude'                  => '',
    'include'                  => '',
    'number'                   => '',
    'taxonomy'                 => 'kategoria-pozycji',
    'pad_counts'               => false
    );
    
    $categories=get_categories($args);
    
    foreach ( $categories as $category ) {
    
    if ( $category->parent > 0 ) {
    continue;
    }
    
    echo '<h1 style="font-weight:bold">' . $category->name . '</h1>';
    
    $querystr = "SELECT $wpdb->posts.*
                  FROM $wpdb->posts, $wpdb->term_relationships, $wpdb->terms
                  WHERE term_id = (" . $category->cat_ID . ")
                  AND term_taxonomy_id = (" . $category->term_taxonomy_id . ")
                  AND ID = object_id
                  AND post_type = 'biblioteka'
                  AND post_status = 'publish'
                  ORDER BY post_date DESC";
    $posts = $wpdb->get_results($querystr, OBJECT);
    
    echo '<ul>';
        foreach ( $posts as $post ) {
            setup_postdata($post);  
    
                echo '<li>'; the_title();   echo '</li>';
    
                }
    echo '</ul>';
    
    $categories2 = get_terms('kategoria-pozycji',array('parent' => $category->term_id , 'hide_empty'=> '0' ));
    
    foreach ( $categories2 as $category ) {
    
    echo '<h2>' . $category->name . '</h2>';
    
    $posts = get_posts( array( 'kategoria-pozycji' => $category->name, 'post_type' => 'biblioteka' ) );  
    
    echo '<ul>';
        foreach($posts as $post) {
            setup_postdata($post);  
    
                echo '<li>'; the_title();   echo '</li>';
    
                }
    echo '</ul>';
    
    }
    }
    
    ?>
Viewing 1 replies (of 1 total)
  • The topic ‘Tree of category and subcategory and the posts titles (custom post type)’ is closed to new replies.