• Resolved wrimomatt

    (@wrimomatt)


    I have a Custom Post Type that creates Bio Pages for Staff members and Detailed descriptions of Services. A taxonomy allows me to link certain employees to certain services. e.g. several employees share a taxonomy with Bookkeeping so that at the bottom of the bookkeeping page I can list the staff assigned to that service.

    Everything works great, but the problem is now my client wants to rank the employees for each Service, so there’s no consistent ordering to work into the query. For every service I need a way to spit out up to 6 employees in a different order depending on the Service.

    Any ideas welcome.

Viewing 1 replies (of 1 total)
  • Thread Starter wrimomatt

    (@wrimomatt)

    Posting my solution if anyone has a similar need or if anyone thinks of a more elegant solution (I’m sure there is one). I would love for someone to come up with a way to make this happen in one template instead of one for each term:

    1. created a template file for each of the taxonomy terms in the CPT from a clone of the main CPT template (e.g. single-cpt-term1.php, single-cpt-term2.php, etc.).
    2. added meta boxes to the CPT to assign a value for each of the terms
    3. ordered by the meta value in each of the individual term templates

    <?php    //get the post's assignment
    $custom_terms = wp_get_post_terms($post->ID, 'your_custom_taxonomy');
    if( $custom_terms ){
    // going to hold our tax_query params
    $tax_query = array();
    // add the relation parameter
    if( count( $custom_terms > 1 ) )
    $tax_query['relation'] = 'OR' ;
    // loop through assignments and build a tax query
    foreach( $custom_terms as $custom_term ) {
    $tax_query[] = array(
    'taxonomy' => 'your_custom_taxonomy',
    'field' => 'slug',
    'terms' => $custom_term->slug,
    );
    
    }
    // put all the WP_Query args together
    $args = array( 'post_type' => 'your_custom_posttype',
    'posts_per_page' => 6,
    'meta_key' => 'your_metakey',
    'order' => 'ASC',
    'orderby' => 'meta_value_num',
    'post__not_in'   => array( get_the_ID() ), // Exclude current post
    'tax_query' => $tax_query );
    
    // finally run the query
    $loop = new WP_Query($args);
    if( $loop->have_posts() ) {
    while( $loop->have_posts() ) : $loop->the_post(); ?>
    					
    <!-- YOUR CONTENT HERE -->
    <?php endwhile;
     }
    wp_reset_query();
    }?>
    • This reply was modified 7 years, 8 months ago by wrimomatt.
    • This reply was modified 7 years, 8 months ago by wrimomatt. Reason: code spacing is weird
Viewing 1 replies (of 1 total)
  • The topic ‘Custom Order Related Pages’ is closed to new replies.