0

I'm currently in the process of building out a site for a legal firm that's a client of ours. We have a two custom post types, attorney's and cases, that we need to link together in various areas throughout the site. For instance, when a case's page is visited, the sidebar should reflect the attorneys that worked that case, and each attorney's page should show what cases they've worked.

I currently have it set up so that each time a new case is created, an entry is made into a custom taxonomy "case log". This way you simply assign an attorney to a case by going into the profile backend and clicking the relevant ones. I've got this working with the latter part of my goal (listing of cases worked in their profile), but have yet to be able to implement the sidebar listings. Here's is the query I'm trying to run...

<?php 

    // $valuec    = get_query_var($wp_query->query_vars['case-log']);
    // echo get_term_by('slug',$value,$wp_query->query_vars['case-log']);
    $terms = get_the_terms( $post->ID, 'case-log' );
    if ( !empty( $terms ) ){
        // get the first term
        $term = array_shift( $terms );
        $casesearch = implode(",", $terms);
        echo $casesearch;
        echo $term->slug;
    }

        $args = array(
             'post_type' => 'attorney',
             'pagename' => $casesearch
        );
        $case_attorney = new WP_Query( $args );

        if ( $case_attorney->have_posts() ) : 
        ?>

            <?php while ( $case_attorney->have_posts() ) : $case_attorney->the_post() ?>
                    <div <?php post_class() ?>>
                        <h2><?php the_title() ?></h2>

                    </div>




                    <?php endwhile ?>
                <?php else : ?>
                    <h2>Ooops, no posts here!</h2>
                <?php endif ?>

With the above I'm able to print out the slug using echo $term->slug;, and the loop runs, but it simply spits out every attorney's profile name, seemingly skipping the second argument.

Any help is greatly appreciated, thanks!

UPDATE: Full code for the sidebar template.

<header class="caseh">Attorneys</header>

<div class="caseattorneys">

    <?php 



    if( is_singular( "case" ) ) {

            //We are in single "case" view
            //Get the ID of current case post
            $case_id = get_queried_object_id();

            // Set "post_type" agument for the query
            $args = array(
                 'post_type' => 'attorney'
            );

            //Get the terms from case-log taxonomy
            //get_the_terms returns an array of terms, false or a WP_Error object
            $terms = get_the_terms( $case_id, 'case-log' );

            if( ! is_wp_error( $terms ) && $terms ) {

                 // Set "tax_query" agument for the query
                 $args['tax_query'] = array(
                     array(
                         'taxonomy' => 'case-log',
                         'terms'    => $terms
                     ),
                 );

            }

            $case_attorney = new WP_Query( $args );


         }

        if ( $case_attorney->have_posts() ) : 
        ?>

            <?php while ( $case_attorney->have_posts() ) : $case_attorney->the_post() ?>
                    <div <?php post_class() ?>>
                        <h2><?php the_title() ?></h2>

                    </div>




                    <?php endwhile ?>
                <?php else : ?>
                    <h2>Ooops, no posts here!</h2>
                <?php endif ?>

</div>

<div class="morefeaturedcases">

    <h3>More Featured Cases</h3>


</div>

<a href="/contact-us/" class="ccu">Contact Us</a>


</aside>
  • Using page slug and pagename argument to request a custom post type based on terms from a custom taxonomy sounds quite wired. Not sure if I understand your question. – cybmeta Apr 6 '15 at 21:56
2

pagename is for request page (core page post type) by slug. What you want, if I understood correctly, is to get "attorney" posts that belongs to same case-log terms that current "case" post. I would do it something like this (not sure where you are going to execute the code, I've tried to code as universally as possible):

if( is_singular( "case" ) ) {

    //We are in single "case" view
    //Get the ID of current case post
    $case_id = get_queried_object_id();

    // Set "post_type" agument for the query
    $args = array(
         'post_type' => 'attorney'
    );

    //Get the terms from case-log taxonomy
    //get_the_terms returns an array of term objects, false or a WP_Error object
    $terms = get_the_terms( $case_id, 'case-log' );

    if( ! is_wp_error( $terms ) && $terms ) {

         $terms_ids = array();

         foreach( $terms as $term ) {
             $terms_ids[] = $term->term_id;
         }

         // Set "tax_query" agument for the query
         $args['tax_query'] = array(
             array(
                 'taxonomy' => 'case-log',
                 'terms'    => $terms_ids
             ),
         );

    }

    $case_attorney = new WP_Query( $args );

    if ( $case_attorney->have_posts() ) {

        while( $case_attorney->have_posts() ) {

            $case_attorney->the_post();
            ?>

            <div <?php post_class() ?>>
                <h2><?php the_title() ?></h2>
            </div>

            <?php

        }

        wp_reset_postdata();

    } else {

        echo "No matching attorney";

    }


 }
  • Thanks Cybmeta. For a bit of additional clarity, I should note this code runs inside the sidebar, outside of the main loop that runs on the page to bring in the case content. I've implemented what you've given me, and it simply returns my no-post else statement. You are correct in your assumption however, the goal is to match the attorney posts to the case-log terms. – Devon Hicks Apr 7 '15 at 14:59
  • Can you show what you have tried exactly? This code should work. – cybmeta Apr 7 '15 at 15:21
  • Here is a codepen with the entire sidebar template. – Devon Hicks Apr 7 '15 at 16:21
  • Please, add the code the question, don't use external services. The cases post type are 'case' or anything else? You didn't provide that information, so I supposed it to be 'case'. If it is not "case" is_singular( "case" ) fails. Check that. Also, note that is_singular( "case" ) only verifies on single "case" view, not in any other pages, for example in cases archive list view. If you need other condition, please clarify; I choosed that condition because of the description in your question when you said "when a case's page is visited". – cybmeta Apr 7 '15 at 17:02
  • My apologies, the code was too long for the reply box, I'll edit my main post with the full code. Yes, the custom post type is case, and this needs to run in the sidebar after the main loop has completed. (The sidebar is pulled in via an include). – Devon Hicks Apr 7 '15 at 17:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.