sorting posts by custom tag on taxonomy template

Hello, I am trying to sort posts on a page by their custom tags. Sounds easy…but failing. This may be hard to explain but I will try. Anyway, the site has a Learning Center called CORPAK University so I created a CPT called corpaku.
Within that CPT I have several custom categories which are basically products we offers instruction on.
I created a template called taxonomy-corpaku.php This works great. When a user chooses the category page from the category menu they see the posts specifically for that Category (Product).
The problem that has stumped me is this:
I would like the posts to be organized on the page based on their custom tags. The posts are tagged either video or resource link. The videos appear in one section and the resource links should appear below in a differently styled section.
I am trying to do this by modifying multiple loops. but when I modify the loop to only display posts with video tag, it shows all posts from all categories with the video tag. i.e. it stops sorting by the original category of that page.

Ive tried for hours on all sorts of ways like changing parameters like this:

$args = array(								'post_type' => 'corpaku',								'corpaku_tag'    => 'u-video',
);
$the_video = new WP_Query( $args );

My last attempt was adding this line of code above the loop:
<?php $the_video = new WP_Query( array( 'corpaku_tag' => 'u-video' ) ); ?>

Is there a different way to do this? Trying to avoid having to create a ton of templates for each category. This is kind of advanced for a newbie so any help will be greatly appreciated. Thanks

  • eschmidtke

    Hi Michael, wow fast reply. Thanks. First off my CPT is a separate file that I require in my functions.php. No plug in or anything.

    Here is a link as an example. The product is called farrell Valve and the only posts I have for that is one Video post and One Resource Link Post. The code is currently displaying a video from the barrel category and a Video from Gastro category which should not be because this is not the gastro category

    Excuse the messed up css. i originally styled these pages as pages and not I have to modify the css as it broke when I created category pages. Functionality is my focus today.

    template code: http://pastebin.com/x9S0WYvD

    sample page: http://www.corpakms.net/category-corpaku/farrell-u/

    Thank you!!!

  • eschmidtke

    Ok Im getting closer. Ive used a conditional statement to style the videos headings in the accordion style and the Resource Link titles as regular links. This is within one loop. Im just not sure how to separate them. I need an orderby solution I think.

    Any luck as to how i can modify the loop to separate by tag? In addition to these tags there will be 2 more, a pro tag and a patient tag. So the post should be divided into AUDIENCE as well as resource links and videos so the end result should be link this:

    FOR PROFESSIONALS
    Resource Links
    Videos

    FOR PATIENTS AND CAREGIVERS
    Resource Links
    Videos

    Here is my later code if you want to look and the live page has been updated. The posts are being listed as they are created which isn't going to work as you can see.

    http://pastebin.com/aYCfY52b

    Thanks!!!

  • eschmidtke

    Ok one more thing and I will stop bugging you. I found this post on stack overflow which sounds like what I am trying to do. Is this a relevant and current solution to my issue? I thought I heard to use WP_Query. It is an old post. I tried to modify the code to suit my needs but I couldn't get anything to display. i don't think i quite understand the code solution provided here. If this is on the right path maybe you can explain a bit more. I am pasting the modified code in past bin.

    forum post: http://stackoverflow.com/questions/1662313/wordpress-split-archives-into-5-sections-based-on-category-and-tags

    my modified code: http://pastebin.com/Q1BYWcza

    Thanks and have a great evening!

  • eschmidtke

    Hello Michael. Hope you are well today. I found this tutorial which may be my solution. I need to add a third taxonomy based on this writers second suggestion at the very bottom of the tut. I posted a request for some help on how to insert a foreach loop inside of another foreach loop but I thought I would paste it hear too in case you can get back to me sooner. I know, I need to learn php. Any instruction on this would be great. Thanks again

    The Tut: http://code.tutsplus.com/tutorials/taxonomy-archives-list-posts-by-a-second-taxonomys-terms--cms-20450

    My Question: I need to add a third taxonomy but based on my efforts trying to implement your first method I think I need to use the second method. I am new at php and not really sure of syntax or how to add the additional foreach loop inside of the first foreach loop. I need to accomplish this:

    For Healthcare Professionals

    Resource Links
    —Pro Resource Link 1
    —Pro Resource Link 2
    Videos
    —Pro Video 1
    —Pro Video 2

    For Patients and Caregivers

    Resource Links
    —Patient Resource Link 1
    —Patient Resource Link 2
    Videos
    — Patient Video 1
    —Patient Video 2

  • Patrick

    Hi there @eschmidtke

    I hope you don't mind me jumping in here.

    If all you want to do is output an archive of posts with a certain tag or taxonomy after another archive list of posts with a different tag/taxonomy, then you simply need to use 2 distinct loops. But you must reset the loop function after the first one using rewind_posts:
    http://codex.wordpress.org/Function_Reference/rewind_posts

  • eschmidtke

    Hi Patrick. Thanks so much for jumping in. I started in that direction but when I would go to modify the loop, my taxonomy page stopped sorting the categories. All posts from all categories would appear on all pages. So in further reading I learned that in order to modify the loop on a taxonomy-{cpt}.php template file I have to write a pre_get_get posts function. Im still working on that, figuring out what Im sorting etc. I think it is still more complicated because on each category page I am dividing the posts into 2 groups and then within those groups dividing them again. So my outline above needs to appear that way in every category page but only relate to that category. Does that make sense? Im in over my head...

  • Ash

    Hello @eschmidtke

    I hope you are well today.

    It is quite difficult to suggest without seeing the scenario live. What I understood so far, you want something like this:

    For Healthcare Professionals

    Resource Links
    —Pro Resource Link 1
    —Pro Resource Link 2
    Videos
    —Pro Video 1
    —Pro Video 2

    For Patients and Caregivers

    Resource Links
    —Patient Resource Link 1
    —Patient Resource Link 2
    Videos
    — Patient Video 1
    —Patient Video 2

    Where For Healthcare Professionals and For Patients and Caregivers are tags. And video and resources are tags too, right?

    In that case, you can do with single loop. How's about running the loop 4 times like the following pseudo code:

    if ( have_posts() ) :
        while ( have_posts() ) : the_post();
            if( current_post_has_tag( 'Healthcare Professionals' ) ){
                if( current_post_has_tag( 'video' ) ){
                    the_title();
                }
            }
        endwhile;
        while ( have_posts() ) : the_post();
            if( current_post_has_tag( 'Healthcare Professionals' ) ){
                if( current_post_has_tag( 'resources' ) ){
                    the_title();
                }
            }
        endwhile;
        while ( have_posts() ) : the_post();
            if( current_post_has_tag( 'Healthcare Professionals' ) ){
                if( current_post_has_tag( 'video' ) ){
                    the_title();
                }
            }
        endwhile;
        while ( have_posts() ) : the_post();
            if( current_post_has_tag( 'Healthcare Professionals' ) ){
                if( current_post_has_tag( 'resources' ) ){
                    the_title();
                }
            }
        endwhile;
    endif;

    Cheers
    Ash

  • eschmidtke

    Hi Ash, thanks so much for the reply and the time and efforts to help me. Yes I think that you are on the right track as far as the overall outline. But these are custom post types. I called them corpaku. I have a custom category called corpaku-audience (healthcare professional or patient) and a custom tag called corpaku-type (resource link or video). I feel like once custom post types are thrown into the mix it does not appear as simple. Below is my code for my CPT file. And remember, this code needs to appear on my taxonomy-corpaku.php file. ANy idea how this should work? thanks again.

    <?php
    
    function custom_post_corpaku() {
    	// creating (registering) the custom type
    	register_post_type( 'corpaku', /* (http://codex.wordpress.org/Function_Reference/register_post_type) */
    	 	// let's now add all the options for this post type
    		array('labels' => array(
    			'name' => __('CORPAK U'), /* This is the Title of the Group */
    			'singular_name' => __('CORPAK U'), /* This is the individual type */
    			'all_items' => __('CORPAK U'), /* the all items menu item */
    			'add_new' => __('Add New CORPAK U'), /* The add new menu item */
    			'add_new_item' => __('Add New CORPAK U'), /* Add New Display Title */
    			'edit' => __( 'Edit'), /* Edit Dialog */
    			'edit_item' => __('Edit CORPAK U'), /* Edit Display Title */
    			'new_item' => __('New CORPAK U'), /* New Display Title */
    			'view_item' => __('View CORPAK U'), /* View Display Title */
    			'search_items' => __('Search CORPAK U'), /* Search Custom Type Title */
    			'not_found' =>  __('Nothing found in the Database.'), /* This displays if there are no entries yet */
    			'not_found_in_trash' => __('Nothing found in Trash'), /* This displays if there is nothing in the trash */
    			'parent_item_colon' => ''
    			), /* end of arrays */
    			'description' => __( 'This is the example custom post type CORPAK U' ), /* Custom Type Description */
    			'public' => true,
    			'publicly_queryable' => true,
    			'exclude_from_search' => false,
    			'show_ui' => true,
    			'query_var' => true,
    			'menu_position' => 7, /* this is what order you want it to appear in on the left hand side menu */
    			'menu_icon' => get_stylesheet_directory_uri() . '/library/images/custom-post-icon.png', /* the icon for the custom post type menu */
    			'rewrite'	=> array( 'slug' => 'corpaku', 'with_front' => false ), /* you can specify its url slug */
    			'has_archive' => true,
    			'capability_type' => 'post',
    			'hierarchical' => false,
    			/* the next one is important, it tells what's enabled in the post editor */
    			'supports' => array( 'title', 'editor', 'author', 'thumbnail', 'excerpt', 'trackbacks', 'custom-fields', 'comments', 'revisions', 'sticky')
    	 	) /* end of options */
    	); /* end of register post type */
    
    	/* this adds your post categories to your custom post type */
    	register_taxonomy_for_object_type('category', 'corpaku');
    	register_taxonomy_for_object_type('post_tag', 'corpaku');
    
    } 
    
    	// adding the function to the Wordpress init
    	add_action( 'init', 'custom_post_corpaku');
    
    	/*
    	for more information on taxonomies, go here:
    	http://codex.wordpress.org/Function_Reference/register_taxonomy
    	*/
    
    	// now let's add custom categories (these act like categories)
        register_taxonomy( 'corpaku_cat',
        	array('corpaku'), /* if you change the name of register_post_type( 'custom_type', then you have to change this */
        	array('hierarchical' => true,     /* if this is true, it acts like categories */
        		'labels' => array(
        			'name' => __( 'CORPAK U Categories', 'awesome-theme' ), /* name of the custom taxonomy */
        			'singular_name' => __( 'CORPAK U Category', 'awesome-theme' ), /* single taxonomy name */
        			'search_items' =>  __( 'Search CORPAK U Categories', 'awesome-theme' ), /* search title for taxomony */
        			'all_items' => __( 'All CORPAK U Categories', 'awesome-theme' ), /* all title for taxonomies */
        			'parent_item' => __( 'Parent CORPAK U Category', 'awesome-theme' ), /* parent title for taxonomy */
        			'parent_item_colon' => __( 'Parent CORPAK U Category:', 'awesome-theme' ), /* parent taxonomy title */
        			'edit_item' => __( 'Edit CORPAK U Category', 'awesome-theme' ), /* edit custom taxonomy title */
        			'update_item' => __( 'Update CORPAK U Category', 'awesome-theme' ), /* update title for taxonomy */
        			'add_new_item' => __( 'Add New CORPAK U Category', 'awesome-theme' ), /* add new title for taxonomy */
        			'new_item_name' => __( 'New CORPAK U Category Name', 'awesome-theme' ) /* name title for taxonomy */
        		),
        		'show_admin_column' => true,
        		'show_ui' => true,
        		'query_var' => true,
        		'rewrite' => array( 'slug' => 'corpaku-category' ),
        	)
        );   
    
    	register_taxonomy( 'corpaku_audience',
        	array('corpaku'), /* if you change the name of register_post_type( 'custom_type', then you have to change this */
        	array('hierarchical' => true,     /* if this is true, it acts like categories */
        		'labels' => array(
        			'name' => __( 'CORPAK U Audience', 'awesome-theme' ), /* name of the custom taxonomy */
        			'singular_name' => __( 'CORPAK U Audience', 'awesome-theme' ), /* single taxonomy name */
        			'search_items' =>  __( 'Search CORPAK U Audience', 'awesome-theme' ), /* search title for taxomony */
        			'all_items' => __( 'All CORPAK U Audience', 'awesome-theme' ), /* all title for taxonomies */
        			'parent_item' => __( 'Parent CORPAK U Audience', 'awesome-theme' ), /* parent title for taxonomy */
        			'parent_item_colon' => __( 'Parent CORPAK U Audience:', 'awesome-theme' ), /* parent taxonomy title */
        			'edit_item' => __( 'Edit CORPAK U Audience', 'awesome-theme' ), /* edit custom taxonomy title */
        			'update_item' => __( 'Update CORPAK U Audience', 'awesome-theme' ), /* update title for taxonomy */
        			'add_new_item' => __( 'Add New CORPAK U Audience', 'awesome-theme' ), /* add new title for taxonomy */
        			'new_item_name' => __( 'New CORPAK U Audience Name', 'awesome-theme' ) /* name title for taxonomy */
        		),
    
        		'show_admin_column' => true,
        		'show_ui' => true,
        		'query_var' => true,
        		'rewrite' => array( 'slug' => 'corpaku-audience' ),
        	)
        );
    
    	// now let's add custom tags (these act like categories)
        register_taxonomy( 'corpaku_type',
        	array('corpaku'), /* if you change the name of register_post_type( 'custom_type', then you have to change this */
        	array('hierarchical' => false,    /* if this is false, it acts like tags */
        		'labels' => array(
        			'name' => __( 'CORPAK U Type', 'awesome-theme' ), /* name of the custom taxonomy */
        			'singular_name' => __( 'CORPAK U Type', 'awesome-theme' ), /* single taxonomy name */
        			'search_items' =>  __( 'CORPAK U Custom Types', 'awesome-theme' ), /* search title for taxomony */
        			'all_items' => __( 'All CORPAK U Types', 'awesome-theme' ), /* all title for taxonomies */
        			'parent_item' => __( 'Parent CORPAK U Type', 'awesome-theme' ), /* parent title for taxonomy */
        			'parent_item_colon' => __( 'Parent CORPAK U Type:', 'awesome-theme' ), /* parent taxonomy title */
        			'edit_item' => __( 'Edit CORPAK U Type', 'awesome-theme' ), /* edit custom taxonomy title */
        			'update_item' => __( 'Update CORPAK U Type', 'awesome-theme' ), /* update title for taxonomy */
        			'add_new_item' => __( 'Add New CORPAK U Type', 'awesome-theme' ), /* add new title for taxonomy */
        			'new_item_name' => __( 'New CORPAK U Type Name', 'awesome-theme' ) /* name title for taxonomy */
        		),
    
        		'show_admin_column' => true,
        		'show_ui' => true,
        		'query_var' => true,
        	)
        ); 
    
    ?>
  • Ash

    Hello @eschmidtke

    I hope you are well today.

    Honestly this is difficult to suggest just seeing the code, it also depends on the active theme.

    Would you please send me your admin login and ftp login and a link where the you want to show the info using taxonomy-corpaku.php file. I will try.

    To send me details, please use our contact form: https://premium.wpmudev.org/contact/

    Select: I have a different question
    Subject: Attn-Ash
    Details: Send all required details (admin info and/or ftp details) with a link of this thread, so that I can track.
    Also post a note here once you send the info.

    I will be happy to take a look :slight_smile:

    Cheers
    Ash

  • eschmidtke

    hi Ash,

    Sorry for the long lapse in keeping up with this issue. I have worked very hard in implementing your code you provided above. Your loop ended up working very well and the posts are organized correctly. Thanks so much. The only thing I have not been able to do is to figure out how to inject headings above each section to break up the 4 groups of posts. I know I need to add conditional code so Headings do not appear if there are no posts in that group. It seems I can get the first one to work but not subsequent sections. Any ideas? Here is my loop so far:

    <?php /* Start the Loop */ ?>
    			<?php if ( have_posts() ) : ?>
    
    			<?php if ( has_tag( 'pro') ) {
    			 	 echo '<h2>' . 'For Healthcare Professionals' . '</h2>';
    
    			} else {
    			  //everything else
    			} ?>
    
    			<ul>
    		    <?php while ( have_posts() ) : the_post();
    		        if( has_tag( 'pro' ) ){ ?>
    
    						<?php if( has_tag( 'url' ) ){
    			               echo '<li>';
    			                the_content();
    			                echo '</li>';
    			            } ?>		            
    
    		        <?php } ?>
    		    <?php endwhile; ?>
    		    </ul>
    
    			<?php if ( has_tag( 'pro') && has_tag( 'video')  ) {
    			 	echo '<h3>' . 'VIDEOOOOOOOOOO' . '</h3>';
    
    			} else {
    			  	echo '<h3>' . 'POOP' . '</h3>';//everything else
    			} ?>
    
    			<div class="accordion">
    		    <?php while ( have_posts() ) : the_post();
    		        if( has_tag( 'pro' ) ){ ?>
    
    		            <?php if( has_tag( 'video' ) ){ ?>
    
    		                <dd>
    							<a href="#panel<?php the_ID(); ?>" title="<?php the_title_attribute(); ?>"> <h4 class="fi-plus "> <?php the_title(); ?></h4></a>
    
    						    <div class="content" id="panel<?php the_ID(); ?>">
    
    							    <?php the_content(); ?>
    
    							    <?php $my_post_meta = get_post_meta($post->ID, 'video_download', true); ?>
    								<?php if ( ! empty ( $my_post_meta ) ) { ?>
    
    								<p><a href="<?php echo wpautop($my_post_meta); ?>">Download video</a></p>
    
    								<?php } ?>
    
    						    <div> <!-- end accordion content -->
    						</dd>
    		            <?php }
    		            }
    		    endwhile; ?>
    		    </div>
    
    		    <?php if ( has_tag( 'patients') ) {
    			 	 echo '<h2>' . 'For Patients and Care Givers' . '</h2>';
    
    			} else {
    			  //everything else
    			} ?>
    
    		    <?php while ( have_posts() ) : the_post();
    		        if( has_tag( 'patient' ) ){ ?>
    
    		            <?php if( has_tag( 'url' ) ){
    			               echo '<li>';
    			                the_content();
    			                echo '</li>';
    			            } ?>
    		       <?php } ?>
    		    <?php endwhile; ?>
    
    		    <div class="accordion">
    		    <?php while ( have_posts() ) : the_post();
    		        if( has_tag( 'patient' ) ){ ?>
    
    		            <?php if( has_tag( 'video' ) ){ ?>
    
    		                <dd>
    							<a href="#panel<?php the_ID(); ?>" title="<?php the_title_attribute(); ?>"> <h4 class="fi-plus "> <?php the_title(); ?></h4></a>
    
    						    <div class="content" id="panel<?php the_ID(); ?>">
    
    							    <?php the_content(); ?>
    
    							    <?php $my_post_meta = get_post_meta($post->ID, 'video_download', true); ?>
    								<?php if ( ! empty ( $my_post_meta ) ) { ?>
    
    								<p><a href="<?php echo wpautop($my_post_meta); ?>">Download video</a></p>
    
    								<?php } ?>
    
    						    <div> <!-- end accodion content -->
    						</dd>
    
    		            <?php } ?>
    
    		        <?php }
    		    endwhile; ?>
    		    </div>
    
    		<?php endif; ?>
    
    			<?php _s_paging_nav(); ?>
    
    		<?php else : ?>
    
    			<?php get_template_part( 'content', 'none' ); ?>
    
    		<?php endif; ?>

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.