WordPress.org

Ready to get started?Download WordPress

Forums

CPT-onomies: Using Custom Post Types as Taxonomies
Listing all custom post type items for each CPT-onomy item (5 posts)

  1. mpmchugh
    Member
    Posted 4 months ago #

    I have this loop, below, in an rchive page, which works great to output all the Publications (custom post type) for a particular Artist (a CPT-onomy), using the archive rewrites described in the documentation.

    How would I use and modify such a loop on a custom Page to lists all Publications for all Artists that have publications, separated by Artist headings?

    I didn't see a similar example in the documentation or another thread that quite illustrates how to structure such a "for each" setup based on a CPT-onomy.

    <?php if ( have_posts() ) : ?>
           <?php
    	$args = array( 'post_type' => 'publications', 'posts_per_page' => -1 );
    	$loop = new WP_Query( $args );
    
    	while ( $loop->have_posts() ) : $loop->the_post(); ?>
    
    		<div class="pubItem">
    			<a href="<?php the_permalink() ?>"><?php the_post_thumbnail( 'publication-thumb' ); ?>
    				<? the_title(); ?>
    				<? the_content(); ?>
    			</a>
    		</div>
    
    	<?php endwhile; ?>
    <?php else : ?>
    <?php endif; ?>

    Thanks,
    Michael

    https://wordpress.org/plugins/cpt-onomies/

  2. mpmchugh
    Member
    Posted 4 months ago #

    So, I found an example elsewhere that led me to the loop below, which works perfectly to pull a listing of Publications sorted and grouped by the Artists CPT-onomy, but I have one question remaining on this regarding ordering the CPT-onomy terms.

    My CPT-onomy is Artists, which are artists names. In places where I just have a loop pulling the artists names as a CPT, like on the Artists archive page, I was able to use this function to order them by last name:

    function posts_orderby_lastname ($orderby_statement)
    {
      $orderby_statement = "RIGHT(post_title, LOCATE(' ', REVERSE(post_title)) - 1) ASC";
        return $orderby_statement;
    }

    However, for the loop in question below, where I'm using Artists as the CPT-onomy, I could not figure out how to order the taxonomy terms by last name at all. I ended up installing the 'Category Order and Taxonomy Terms Order' plugin that let me order the terms manually, and then use 'orderby' => 'term_order' in the tax_query array.

    <?php if ( have_posts() ) : ?>
    <?php
    $custom_terms = get_terms('artists');
    
    foreach($custom_terms as $custom_term) {
        wp_reset_query();
        $args = array(
        	'post_type' => 'publications',
        	'posts_per_page' => -1,
            'tax_query' => array(
                array(
                    'taxonomy' => 'artists',
                    'field' => 'slug',
                    'terms' => $custom_term->slug,
            		'orderby' => 'term_order',
            		'order' => 'ASC',
                ),
            ),
            'orderby' => 'name',
            'order' => 'ASC',
         );
    
    	$loop = new WP_Query( $args );
    
           if($loop->have_posts()) {
            echo '<h2 class="artistName">'.$custom_term->name.'</h2>';
    
    			while ( $loop->have_posts() ) : $loop->the_post(); ?>
    		<div class="pubItem">
    			<a href="<?php the_permalink() ?>"><?php the_post_thumbnail( 'publication-thumb' ); ?>
    				<? the_title(); ?>
    				<? the_content(); ?>
    			</a>
    		</div>
    
    	<?php endwhile;
    		}
    	}?>
    <?php else : ?>
    <?php endif; ?>

    This is working, but I'm just wondering if there's anyway to adapt and integrate something like the "posts_orderby_lastname" to order the taxonomy by last name programmatically, so the client doesn't have to manually maintain their order.

    Thanks,
    Michael

  3. Rachel Carden
    Member
    Plugin Author

    Posted 3 months ago #

    I'm confused. You want to sort the list of publications by the last name of the artist? But it's listed underneath the artist's name. Do you have some kind of example you can show me to help me better understand?

  4. mpmchugh
    Member
    Posted 3 months ago #

    No, not the publications themselves. I just want to sort the list of Artists by last name, with their publications alphabetically below them. This is the page in question, which uses the loop I quoted above.

    http://galleryluisotti.df-dev.com/publications-directory/

    I tried using the 'Category Order and Taxonomy Terms Order' plugin that let me order the terms manually, and then use 'orderby' => 'term_order' in the tax_query array, which seemed to work, but is a bit wonky, and has to be maintained manually anyway, so I was wondering if there's a way to integrate some kind of 'posts_orderby_lastname' function into the loop instead.

    For this Artists listing, I was able to use the 'posts_orderby_lastname' function method described here: http://stackoverflow.com/a/16418806 to get this: http://galleryluisotti.df-dev.com/artists/

    That worked fine. However, for the Publications page, which should list all publication CPTs sorted by the Artists CPT, I can't figure how to order the Artists tax by last name in the loop — something similar to the 'posts_orderby_lastname' function.

    Does that make sense?

  5. mpmchugh
    Member
    Posted 3 months ago #

    Still looking for a solution to this.

    I'd prefer a method of automatically sorting a loop by last name.

    Barring that, I've tried two Taxonomy ordering plugins, but neither seems to work with CPT-onomies.

    The two I've tried are 'Category Order and Taxonomy Terms Order' and 'Custom Taxonomy Order NE'. Is there a reason neither of these would work?

    Any other posibilities?

    Thanks,
    Michael

Reply

You must log in to post.

About this Plugin

About this Topic

Tags

No tags yet.