• Resolved FrankG2013

    (@frankg2013)


    Hello,

    I would like to show my custom post type ordered by category (custom taxonomy)on a page, if possible also with the category name above the posts. But i dont know how i can do this. does anyone know the solution for this?

    <?php $my_query = new WP_Query( $args = array(
    'post_type' => 'cupcakes',
    'taxonomy' => 'cupcake_category',
    'field' => 'slug',
    'terms' => 'specials')
    ); 
    
    $query = new WP_Query( $args ); ?>
    
    <?php get_header(); ?>
    <section id="primary" class="content-area">
    <div id="content" class="site-content" role="main">    
    
    <?php if ($my_query-> have_posts() ) : ?>
    <header class="page-header">
    <h1 class="page-title"></h1>
    </header><!-- .page-header -->
    
    <div class="archive-posts">
      <header class="entry-header">
    	<h1 class="entry-title"></h1>
    		</header><!-- .entry-header -->
    			<?php /* Start the Loop */ ?>
    <?php while ($my_query-> have_posts() ) : $my_query->the_post(); ?>
    
    <?php get_template_part( 'content', 'page-cupcakes');?>
    
    <?php endwhile; ?>
    
    </div>
    </div>
    <?php snaps_content_nav( 'nav-below' ); ?>
    
    <?php else : ?>
    
    <?php get_template_part( 'no-results', 'archive' ); ?>
    
    <?php endif; ?>
    </div><!-- #content .site-content -->
    </section><!-- #primary .content-area -->
    <?php get_footer(); ?>

    This is how it looks so far (this is a created archive-cupcake.php file)

Viewing 7 replies - 1 through 7 (of 7 total)
  • Moderator bcworkz

    (@bcworkz)

    As your code stands there would be nothing unique to sort by because you only specify one term. It is possible to query multiple terms, so your question is still valid. For more on taxonomy queries see
    Class_Reference/WP_Query#Taxonomy_Parameters

    It will not be easy to sort by term slug unless you are good at mySQL queries. To order by taxonomy terms you need the terms table joined into the query. This is not even done for a taxonomy query, so you almost need to create your own query to do this. If you hook the ‘posts_request’ filter just to examine the resulting query, this gives you a starting point. The rest is up to you, you could either go ahead and filter the query, or use $wpdb global object to do your own query.

    Sorting by term ID is a different matter, change the ORDER BY clause to wp_term_relationships.term_taxonomy_id

    FWIW, assigning meta values instead of taxonomy terms makes things much easier to order by. Orderby Parameters

    Thread Starter FrankG2013

    (@frankg2013)

    bcworkz thanks for your reply.

    it is also the intention to use the term slug, I was actually planning to do it with a multiple query. but I do not get that done. Is this possible on one page? and if so….how?

    it should be three predetermined terms. “specials”, “standard” and “seasonal”

    Moderator bcworkz

    (@bcworkz)

    Yes, use the ‘tax_query’ argument instead of the individual taxonomy, field, terms arguments.

    'tax_query' => array(
    	array(
    		'taxonomy' => 'cupcake_category',
    		'field'    => 'slug',
    		'terms'    => array('specials', 'standard', 'seasonal',),
    	),
    ),

    Note the above code is but one argument in an array of query arguments.

    Sadly, sorting by those terms is not so easy. A quick workaround would be to fiddle with the term IDs so they are ordered as desired, then using the suggested term ID ordering from my last post. This involves phpMyAdmin and an understanding of the taxonomy table relationships – not so easy either.

    I’ve done this by sorting the items after I run the WordPress query.

    Run the query, and grab the “Posts” array off the WP_Query object. Take that array and sort it using the PHP usort function (http://php.net/manual/en/function.usort.php), and THEN run it through your display function.

    You can sort based on whatever criteria you need for each post, including category. Your special sort function will make the decision of how to sort when there are multiple categories on posts, and which one should take precedence.

    If you don’t have a lot of categories, you can also just do one query for each category, sorted by title.

    Thread Starter FrankG2013

    (@frankg2013)

    bcworkz & Ancawonka thanks again,

    Ancawonka you might have an example of this? sorry, but this is a little bit of abracadabra for me;-)

    Thread Starter FrankG2013

    (@frankg2013)

    I still struggle with above problem……someone else here can help me? I would like to sort on a archive page, my created ‘post_type’ => ‘cupcakes’ with the ‘taxonomy’=> ‘cupcake_category’.

    How can i show the selected cupcakes listed for example by: Specials, Normal and Seasonal (selected cupcake category)

    Maybe there is someone with an example of this? or a good explanation?

    Thread Starter FrankG2013

    (@frankg2013)

    pfffff! I made a few small changes and it works now! forget my last post! thanks anyway

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘custom post type ordered by category (custom taxonomy) on a page’ is closed to new replies.