WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] how to sort post by selected custom taxonomy (2 posts)

  1. Tommy Pradana
    Member
    Posted 1 year ago #

    Im sorry, i have no idea to sort my custom post type post, called "album", and the taxonomy called "album_category"

    add_action('init', 'tm_album_posttype_register');
    function tm_album_posttype_register() {
    	$labels = array(
    		'name' => _x('Albums', 'post type general name','Dzaky'),
    		'singular_name' => _x('Album', 'post type singular name','Dzaky'),
    		'add_new' => _x('Add New', 'album item','Dzaky'),
    		'add_new_item' => __('Add New Album','Dzaky'),
    		'edit_item' => __('Edit Album','Dzaky'),
    		'new_item' => __('New Album','Dzaky'),
    		'all_items' => __('All Albums','Dzaky'),
    		'view_item' => __('View Album','Dzaky'),
    		'search_items' => __('Search Albums','Dzaky'),
    		'not_found' =>  __('Nothing found','Dzaky'),
    		'not_found_in_trash' => __('Nothing found in Trash','Dzaky'),
    		'parent_item_colon' => ''
    	);
    
    	$args = array(
    		'labels' => $labels,
    		'public' => true,
    		'publicly_queryable' => true,
    		'show_ui' => true,
    		'query_var' => true,
    		'rewrite' => apply_filters( 'tm_album_posttype_rewrite_args', array( 'slug' => 'album', 'with_front' => false ) ),
    		'capability_type' => 'post',
    		'hierarchical' => false,
    		'menu_position' => 5,
    		'menu_icon' => get_template_directory_uri().'/images/settings-market.png',
    		'supports' => array('title','editor','thumbnail','excerpt','comments','revisions','custom-fields')
    	);
    
    	register_post_type( 'album' , $args );
    
    }
    
    add_action('init', 'album_manager_register');
    
    function album_manager_register() {
    
    	$labels = array(
    		'name' => _x('Album Manager', 'post type general name'),
    		'singular_name' => _x('Album Manager item', 'post type singular name'),
    		'add_new' => _x('Add New', 'album manager item'),
    		'add_new_item' => __('Add New Portfolio Item'),
    		'edit_item' => __('Edit Album Manager'),
    		'new_item' => __('New Album Manager Item'),
    		'view_item' => __('View Album Manager Item'),
    		'search_items' => __('Search Album Manager'),
    		'not_found' =>  __('Nothing found'),
    		'not_found_in_trash' => __('Nothing found in Trash'),
    		'parent_item_colon' => ''
    	);
    
    	$args = array(
    		'labels' => $labels,
    		'public' => true,
    		'publicly_queryable' => true,
    		'show_ui' => true,
    		'query_var' => true,
    		'menu_icon' => get_stylesheet_directory_uri() . '/article16.png',
    		'rewrite' => true,
    		'capability_type' => 'post',
    		'hierarchical' => false,
    		'menu_position' => 100,
    		'supports' => array('title','editor','custom-fields','thumbnail')
    	  ); 
    
    	register_post_type( 'albummanager' , $args );
    }
    
    add_action( 'init', 'tm_create_album_taxonomies', 0 );
    function tm_create_album_taxonomies(){
    	$labels = array(
    		'name' => _x( 'Categories', 'taxonomy general name', 'Dzaky' ),
    		'singular_name' => _x( 'Category', 'taxonomy singular name', 'Dzaky' ),
    		'search_items' =>  __( 'Search Categories', 'Dzaky' ),
    		'all_items' => __( 'All Categories', 'Dzaky' ),
    		'parent_item' => __( 'Parent Category', 'Dzaky' ),
    		'parent_item_colon' => __( 'Parent Category:', 'Dzaky' ),
    		'edit_item' => __( 'Edit Category', 'Dzaky' ),
    		'update_item' => __( 'Update Category', 'Dzaky' ),
    		'add_new_item' => __( 'Add New Category', 'Dzaky' ),
    		'new_item_name' => __( 'New Category Name', 'Dzaky' ),
    		'menu_name' => __( 'Category', 'Dzaky' )
    	);
    
    	register_taxonomy('album_category',array('album'), array(
    		'hierarchical' => true,
    		'labels' => $labels,
    		'show_ui' => true,
    		'query_var' => true,
    		'rewrite' => array( 'slug' => apply_filters( 'dzaky_rewrite_category_slug', 'album-category' ) )
    	));
    
    	$labels = array(
    		'name' => _x( 'Tags', 'taxonomy general name','Dzaky' ),
    		'singular_name' => _x( 'Tag', 'taxonomy singular name','Dzaky' ),
    		'search_items' =>  __( 'Search Tags','Dzaky' ),
    		'popular_items' => __( 'Popular Tags','Dzaky' ),
    		'all_items' => __( 'All Tags','Dzaky' ),
    		'parent_item' => null,
    		'parent_item_colon' => null,
    		'edit_item' => __( 'Edit Tag','Dzaky' ),
    		'update_item' => __( 'Update Tag','Dzaky' ),
    		'add_new_item' => __( 'Add New Tag','Dzaky' ),
    		'new_item_name' => __( 'New Tag Name','Dzaky' ),
    		'separate_items_with_commas' => __( 'Separate tags with commas','Dzaky' ),
    		'add_or_remove_items' => __( 'Add or remove tags','Dzaky' ),
    		'choose_from_most_used' => __( 'Choose from the most used tags','Dzaky' ),
    		'menu_name' => __( 'Tags','Dzaky' ),
    	); 
    
    	register_taxonomy('album_tag','album',array(
    		'hierarchical' => false,
    		'labels' => $labels,
    		'show_ui' => true,
    		'update_count_callback' => '_update_post_term_count',
    		'query_var' => true,
    		'rewrite' => array( 'slug' => apply_filters( 'dzaky_rewrite_tag_slug', 'album-tag' ) )
    	));
    }

    i have this code, to sort post by selected category

    <?php $recent_categories = is_array(tm_get_option('radis_special_recents_news_cat')) ? tm_get_option('radis_special_recents_news_cat') : array();
    			$i=1; ?>
    
    <?php
    foreach ( $recent_categories as $category_id ) { ?>
    <?php if ( $i == 1 || ( ($i-1) % 4 == 0 ) ) { $div_open = true; ?>
    <div class="clearfloat clearfix">
       <?php } ?>
    	<div id="cat-<?php echo $cp_pC; ?>" class="box<?php if ( $i%4 == 0 ) echo ' last'; ?>">
                  <div class="box-title">
                       <h3><a href="<?php echo get_category_link($category_id); ?>"><?php echo get_cat_name( $category_id ); ?></a></h3>
                	</div>
    	<?php $recent_radis_args=array(
    									'showposts'=> 2,
    	'cat' => $category_id,);
    
    								$recent_radis_query = new WP_Query($recent_radis_args); while ( $recent_radis_query->have_posts() ) : $recent_radis_query->the_post(); ?>
    								       <article class="box-content clearfix">
       <h2 class="title"><a href="<?php the_permalink(); ?>" title="<?php the_title();?>"><?php the_title(); ?></a></h2>
    </article><!--end .box-content -->
    
    <?php endwhile; wp_reset_postdata(); ?>
    	</div><!-- .home-block -->
    <?php if ( $i % 4 == 0 ) { $div_open = false; ?>
    </div> <!-- .clearfloat -->
    		<?php } ?>
    	<?php $i++; ?>
    <?php } ?>
    
    <?php if ( isset( $div_open ) && $div_open ) { ?>
    	</div> <!-- .clearfloat -->
    <?php } ?>

    This code work perfectly, if i use default post type "post", the html output for this code :

    Title Categoy
    	<li>title post 1</li>
    	<li>title post 2</li>
    
     Title Categoy 2
    	<li>title other post 1</li>
    	<li>title other post 2</li>

    i have try to change

    <?php $recent_radis_args=array(
    									'post_type' => 'album',
    	'showposts'=> 2,
    	'tax_query' => array(
    		'taxonomy' => 'album_category',
    		'field' => 'id',
    		'terms' => $category_id->term_ID,
    
    					)
    					);			$term_posts = get_posts($recent_radis_args);
    foreach ($term_posts as $post) : setup_postdata($post);?>
    								<article class="box-content clearfix">
    <h2 class="title"><a href="<?php the_permalink(); ?>" title="<?php the_title();?>"><?php the_title(); ?></a></h2>
    </article><!--end .box-content -->
    <?php endforeach; ?>

    but the html output, repeating the same category

    same category 1
    	<li>title same</li>
    	<li>title same 2</li>
    same category 1
    	<li>title same</li>
    	<li>title same 2</li>

    in addition, I want to show ​​it in the single-albummanager.php.

    I hope my friends understand what I mean, sorry if my english is not good

  2. Tommy Pradana
    Member
    Posted 1 year ago #

    Okay, i have solved the problem of my code,

    here it is

    <?php
    $template_settings = array();
    $template_settings = maybe_unserialize( get_post_meta($post->ID,'albummanager_settings',true) );
    $albummanager_cats = isset( $template_settings['albummanager_cats'] ) ? (array) $template_settings['albummanager_cats'] : array();
    $albummanager_showposts = isset( $template_settings['albummanager_showposts'] ) ? (int) $template_settings['albummanager_cats'] : 10;
    ?>
        		<?php $recent_categories = is_array($albummanager_cats) ? $albummanager_cats : array();
    			$i=1; ?>
    
    			<?php
    				foreach ( $recent_categories as $category_id ) {
    				$term = get_term( $category_id, 'album_category' ); ?>
    					<?php if ( $i == 1 || ( ($i-1) % 4 == 0 ) ) {
    						$div_open = true;
    					?>
    						<div class="clearfloat clearfix">
    					<?php } ?>
    							<div id="cat-<?php echo $category_id; ?>" class="box<?php if ( $i%4 == 0 ) echo ' last'; ?>">
                                 <div class="box-title">
                                	<h2><a href="<?php echo get_term_link( $term, 'album_category' ); ?>"><?php echo $term->name; ?></a></h2>
                				 </div>
    								<?php $recent_radis_args=(array(
    				 						'post_type' => 'album',
    										'tax_query' => array(
    											array(
    												'taxonomy' => 'album_category',
    												'terms' => $category_id,
    												)
    											)
    										)
    								);
    								$recent_radis_query = new WP_Query($recent_radis_args);
    								while ( $recent_radis_query->have_posts() ) : $recent_radis_query->the_post(); ?>
    									<article class="box-content clearfix">
            								<h2 class="title"><a href="<?php the_permalink(); ?>" title="<?php the_title();?>"><?php the_title(); ?></a></h2>
                						</article><!--end .box-content -->
    								<?php endwhile; wp_reset_postdata(); ?>
    							</div> <!-- .home-block -->
    					<?php if ( $i % 4 == 0 ) {
    						$div_open = false;
    					?>
    						</div> <!-- .clearfloat -->
    					<?php } ?>
    				<?php $i++; ?>
    			<?php } ?>
    
    			<?php if ( isset( $div_open ) && $div_open ) { ?>
    				</div> <!-- .clearfloat -->
    			<?php } ?>

Topic Closed

This topic has been closed to new replies.

About this Topic