Using PHP retrieve the Custom Post Type category info

Hi, I'm slowly trying to get used to cobbling together the php I need to do custom things within wordpress. I'm using Custom Press to have a 'job' type that has four categories: location, industry, salary bracket and job type.

I'm also a Thesis user so I'm using a custom hook to ensure that each custom category will show up beneath each Job Title on the archive pages. I've gotten this to mostly work in that only on Job archive pages will the word Location show up beneath each and every job post.

/* Add / Show Custom Categories On Archive Page for each Job */
function addLocation() {
global $addCat_Location;
if (is_archive() && get_post_type() == 'job') {
?>
Location: <?php the_category(', '); ?>
<?php
}
}
add_action('thesis_hook_after_headline', 'addLocation');

Here's where I need your help:

I can't seem to get any code I've tried to retrieve and show the category information relevant to that post. How do I do this? I figure I need to retrieve the post ID so that I can use that to query against that posts Categories and show the relevant categories for Location, Industry, Salary Bracket & Job Type.

$postid = get_the_ID();

How do I write: Where Category = Location && $ postid return the value of Category in PHP & wordpress code?

Thank you.
Cathie

  • CathieHeart

    How would I use the implode function in the code I updated from your original one?

    /* Add / Show Custom Categories On Archive Page for each Job */
    function addIndustry() {
    if (is_archive() && get_post_type() == 'job') {
    $tax_industry = 'industry';
    $result_industry = get_the_terms( $post->ID, $tax_industry );
    $tax_location = 'location';
    $result_location = get_the_terms( $post->ID, $tax_location );
    $tax_jobtype = 'job_type';
    $result_jobtype = get_the_terms( $post->ID, $tax_jobtype );
    $tax_salary = 'salary_band';
    $result_salary = get_the_terms( $post->ID, $tax_salary );
    
    foreach($result_industry as $term) {
         echo '<p class="headline_meta"><i>Industry:</i><a class="archiveResult" href="' .get_term_link($term->slug, $tax_industry) .'">'.$term->name.'</a></p>';
    }
    foreach($result_location as $term) {
         echo '<p class="headline_meta"><i>Location:</i><a class="archiveResult" href="' .get_term_link($term->slug, $tax_location) .'">'.$term->name.'</a></p>';
    }
    foreach($result_jobtype as $term) {
         echo '<p class="headline_meta"><i>Job Type:</i><a class="archiveResult" href="' .get_term_link($term->slug, $tax_jobtype) .'">'.$term->name.'</a></p>';
    }
    foreach($result_salary as $term) {
         echo '<p class="headline_meta"><i>Salary:</i><a class="archiveResult" href="' .get_term_link($term->slug, $tax_salary) .'">'.$term->name.'</a></p>';
    }
    }
    }
    add_action('thesis_hook_after_headline', 'addIndustry');

    Thank you so much for your help, I really appreciate it.

  • CathieHeart

    I managed to finally figure out all the code you sent me and adapted it to what I needed to show all four categories with the comma's in between.

    /* Add / Show Custom Categories On Archive Page for each Job */
    function addIndustry() {
    if (is_archive() || is_single() && get_post_type() == 'job') {
    $tax_industry = 'industry';
    $result_industry = get_the_terms( $post->ID, $tax_industry );
    $tax_location = 'location';
    $result_location = get_the_terms( $post->ID, $tax_location );
    $tax_jobtype = 'job_type';
    $result_jobtype = get_the_terms( $post->ID, $tax_jobtype );
    $tax_salary = 'salary_band';
    $result_salary = get_the_terms( $post->ID, $tax_salary );
    
    foreach($result_industry as $term) {
         echo '<p class="headline_meta"><i>Industry: </i><a class="archiveResult" href="' .get_term_link($term->slug, $tax_industry) .'">'.$term->name.'</a></p>';
    }
    ?><p class="headline_meta"><i>Location:</i><?php
    foreach($result_location as $term) {
         $location[] = ' <a class="archiveResult" href="' .get_term_link($term->slug, $tax_location) .'">'.$term->name.'</a>';
    }
    $output_loc = implode(",", $location);
    echo $output_loc;
    ?></p><?php
    foreach($result_jobtype as $term) {
         echo '<p class="headline_meta"><i>Job Type: </i><a class="archiveResult" href="' .get_term_link($term->slug, $tax_jobtype) .'">'.$term->name.'</a></p>';
    }
    ?><p class="headline_meta"><i>Salary: </i><?php
    foreach($result_salary as $term) {
         $salary[] = '<a class="archiveResult" href="' .get_term_link($term->slug, $tax_salary) .'">'.$term->name.'</a>';
    }
    $output_salary = implode(", ", $salary);
    echo $output_salary;
    ?></p><?php
    }
    }
    add_action('thesis_hook_after_headline', 'addIndustry');

    Thank you, you can see it in action here: http://clientsof.cathiegraphy.com/HuntersRecruitment/industry/banking/

    Now to do some more tweaks and add in sections.
    <3

  • CathieHeart

    Hi, I'd like a bit more help please with trying to get this to work the way that I want it to please.

    I've two things I'd like to work better than they currently do and I'm completely stuck on trying to get it t work.
    Issue 1. I'd like for the custom field salary to be shown but when I input the code below it doesn't work.
    echo get_post_meta( $post->ID, 'ct_Salary_text_c0db', true );

    Issue 2: The data only shows up on single page posts and not on searches or archive / category pages. I thought my code was correct but it isn't working to reveal them on those pages.
    if (is_archive() || is_single() || is_category() && get_post_type() == 'job')

    I've posted the entire code below so you can see what I'm working with. Also you can see the live version running on my client servers: http://clientsof.cathiegraphy.com/HuntersRecruitment/industry/banking/ <-- category page
    http://clientsof.cathiegraphy.com/HuntersRecruitment/job/motor-claims-auditor-2/ <-- Single Page

    Thank you and I really appreciate your help.
    Cathie.

    /* Add / Show Custom Categories On Archive Page or Single Post for each Job */
    function addIndustry() {
    if (is_archive() || is_single() || is_category() && get_post_type() == 'job') {
    $tax_industry = 'industry';
    $result_industry = get_the_terms( $post->ID, $tax_industry );
    $tax_location = 'location';
    $result_location = get_the_terms( $post->ID, $tax_location );
    $tax_jobtype = 'job_type';
    $result_jobtype = get_the_terms( $post->ID, $tax_jobtype );
    $tax_salary = 'salary_band';
    $result_salary = get_the_terms( $post->ID, $tax_salary );
    
    foreach($result_industry as $term) {
         echo '<p class="headline_meta"><i>Industry: </i><a class="archiveResult" href="' .get_term_link($term->slug, $tax_industry) .'">'.$term->name.'</a></p>';
    }
    ?><p class="headline_meta"><i>Location:</i><?php
    foreach($result_location as $term) {
         $location[] = ' <a class="archiveResult" href="' .get_term_link($term->slug, $tax_location) .'">'.$term->name.'</a>';
    }
    $output_loc = implode(",", $location);
    echo $output_loc;
    ?></p><?php
    foreach($result_jobtype as $term) {
         echo '<p class="headline_meta"><i>Job Type: </i><a class="archiveResult" href="' .get_term_link($term->slug, $tax_jobtype) .'">'.$term->name.'</a></p>';
    }
    ?><p class="headline_meta"><i>Salary Bracket: </i><?php
    <strong>/* echo get_post_meta( $post->ID, 'ct_Salary_text_c0db', true ); COMMENTED OUT AS IT DOESN'T WORK */</strong>
    
    foreach($result_salary as $term) {
         $salary[] = '<a class="archiveResult" href="' .get_term_link($term->slug, $tax_salary) .'">'.$term->name.'</a>';
    }
    $output_salary = implode(", ", $salary);
    echo $output_salary;
    ?></p><?php
    }
    }
    add_action('thesis_hook_after_headline', 'addIndustry');
  • Patrick

    Hi Cathie,

    First, thanks for cobbling this code together! I'm sure I can find a use for it myself :slight_smile:

    For custom post types, try is_singular instead of is_single
    http://codex.wordpress.org/Function_Reference/is_singular

    For cpt archives, try is_post_type_archive instead of is_archive
    http://codex.wordpress.org/Function_Reference/is_post_type_archive

    For your function to output on search pages, try adding is_search()

    See the bottom of this page for all cpt functions
    http://codex.wordpress.org/Post_Types

    There's also this little plugin that could (a) get the job done for you, or (b) show you its guts and give you insight.

    Hope this helps! :slight_smile:

  • CathieHeart

    Hi PCWriter .. thank you for helping me. Unfortunately none of it is working for me and I'm not quite sure why.

    The code is_single() and && get_post_type() == 'job' are the only bits that work. The is search and is post type archive along with is_category and is_archive don't get the same data to display and its frustrating the heck out me!!

    if (is_post_type_archive() || is_single() || is_search() && get_post_type() == 'job')

    Does anyone else use Thesis and CustomPress and manage to display taxonomy data below the title?

    Thanks!
    Cathie

  • aecnu

    Greetings everyone,

    Just a heads up that an update of Custom Press has just been released by our new developer for Custom Press with several fixes and it is in your best interest to update.

    @Cathie make a backup of your modified files so you can replace them after the update.

    It may also be in your best interest to compare those very files to the updated files so that you do miss any critical new coding. Personally I use Ultra Compare for this exact purpose.

    Cheers, Joe

  • aecnu

    Greetings Everyone,

    Just checking if this issue was eventually resolved in another thread? Or by yourself separately to us? Or by us over email with you? Or using our live support?

    If so, no need to reply, that's great news.

    If not, or you have any more questions related to this thread, please feel free to post them below including any new symptoms or errors and tick the 'Mark as Not Resolved (re-open)' box below the post area (or else we'll miss it!)

    Otherwise, happy days, glad you got it sorted :slight_smile:

    Thank you for being a WPMU Dev member!

    Cheers, Joe

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.