Home Forums General Issues Post Object help


Post Object help

  • Here’s what I’d like to have happen:
    1) Create “Employee” custom post type
    2) Create page template for “Leadership” page
    3) Create Custom Field Group for Employee that has Name, Title, URL, Location, Photo
    4) Populate post type with all the employees.
    5) Leadership page now shows list of employees and their title
    6) Create a page for a specific employee, choosing “Employee” page template from the template drop-down. However, all I input here is their Bio.
    7) After saving page, I go to view it, and it looks like the individual employee page, with their photo, name and title, location and url, and bio showing.

    I’ve currently done through Step 4 above. I also gave the Custom Field Group rules to show if post type is employee or page template is employee.

    While I understand how to make a page like the Leadership that just lists everything, I’m not sure how to make an individual that would just take data from one specific custom post type entry to use. So, say in Step 6 above I make a page for Bill Gates. All that’s on the page is the Bio. How would the individual page (or its template) know which employee to choose to generate/display the custom field groups?

    My only thought is if somehow the individual page template had a dropdown box where you chose an employee (which would come from the custom post type) and it somehow passed that (maybe its ID?) to the page template code, generating the rest of the content.

    With this in mind, I came across the post object field, but I’m not sure I understand how I would actually use it in practice. I added it to the custom field group for Employee (Step 3 above). Now, when I go to Add New Page and choose the Employee template, I see a drop-down of Employees I’ve made in the custom post group. Awesome! However, I also see all those other fields, too. I’d like to just see the Content Editor and have the drop-down to select an employee be what pulls in the fields automatically using the page template. However, I don’t know what the code in the page template should be for that.
    Similarly, with the post obejct field added, when I go to create a new Employee in the custom post type, I have all the fields I made (which I want), but I also have the post object field there (which I don’t want, since that should be for the page template).

    I hope this makes enough sense. If anyone can advise me on this, or even if you can think of a better way of having just one location where all the data fields I’ve mentioned are centralized, but are still somehow able to be pulled in a way where 1) all of the Names/Titles display on a Leadership page AND 2) just one group of the custom fields display on an individual Employee page, I’m all ears!

  • WordPress handles a lot of this for you automatically.

    For your Leadership page that could be the archive template for your Employee custom post type –

    You can say whether or not a custom post type has an archive, and what the URL should be when register the custom post type:

    So if you set your custom post type slug to “employees” and go to it will use the archive template automatically.

    Otherwise you could create a shortcode that does a get_posts on your employee custom post type and display it anywhere.

    For your individual employee pages these are automatically created as well, and use the template:

    So in your single employee template you’ll just need to call get_field or the_field as normal and it will output the values for that specific employee. No need to create a new page and assign employees, as once you’ve created a new employee post it will automatically use this page.

  • This looks like an interesting way of handling this. I’m not sure what’s going on with the archives, though. I’m getting a Page Not Found when I go to what should be the archive page (per your post above), though. I checked my functions.php and see that archive is set to true for my employee post type, and I also added the rewrite slug code you linked to, so I’m not sure what the deal is there. Any ideas?

  • Sometimes that’s due to permalink caching. If you go to Settings > Permalinks and just hit “Save Changes” (don’t need to actually make any changes) it will flush the cache and the URLs should be accessible.

  • Thanks, that worked for getting the archive to display. I played around with it and got the single employee page to work just fine. However, the overall page that lists all employees, it’s not bad, in that it’s listing the employees one after the other on their own line, single-column. I can click their name to go their single page, and that one seems fine if i just style it (the get_field query is working just fine) but, for the overall employees list page, I’d like its presentation to be two-column, with each employee’s title under their name (title not linked, just their name is linked). I tried this with this syntax:

    <h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
    <?php get_field('employee_title'); ?>

    The archive page is ignoring the get_field and still just displaying only the permalink. Do you know what’s going on there? I figure if I can get the title to display under an employee name, well then I can style it to be two-column thereafter. I’ve attached the archive file for reference.

    Thanks again for your efforts thus far.

    EDIT: Also, while I’m waiting for a reply, I’m noticing some of the employees I’m adding here don’t have a URL for LinkedIn. On their single pages, since it’s a template, it’s still generating a hyperlink, but it’s not pointing to anywhere. Is there a syntax I can toss into the single page archive that says something like “if (field) is blank, don’t display this section?”

  • Ah, so get_field does not output the value, to do that you’ll need to either echo it or use the_field instead:

    <h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
    <?php echo get_field('employee_title'); ?>


    <h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
    <?php the_field('employee_title'); ?>

    For doing conditional fields you’ll just need to wrap it in a conditional like:

    <?php if ( $linkedin = get_field( 'linkedin' ) ): ?>
    <a href="<?php echo $linkedin; ?>">View LinkedIn</a>
    <?php endif; ?>
  • Yep, that definitely worked (both of them)!

    Last question then that I’ll ask before closing this out is how I’d make the archive page be two columns, such that it would generate/display content like so:

    employee1 employee2

    employee3 employee4

    I started messing around with it to try to get that but it seems to just list them one after the other in a single column or, if I try to code in a second column, it just repeats the data in that one.

  • This can be done just through the stylesheet. So say for example you have the following in your archive file:

    <?php if (have_posts()) : while (have_posts()) : the_post(); ?>
      <div class="entry-content">
         Content outputting here... 
    <?php endwhile; ?>
    <?php endif; ?>

    I believe each element should be wrapped in something already, like a <div> or <article> or similar. You’ll just target that with your CSS and make it two-columns. So for the example snippet above you’d probably have some CSS like:

    .archive-employees .entry-content {
      float: left;
      width: 50%;

    That .archive-employees bit is whatever class is currently on the <body> element on your archive page. Might need some tweaking but that’s the general idea.

  • Awesome, that worked. Thanks so much for your help all day!

  • Going to jump in here again and ask how I can make that employee archive post in a set order… I added three employees, but they display in different positions/order than they should. I manually edited the Order field (1, 2, 3) for each entry, but to no avail.

    EDIT: Found this and it worked!

Viewing 10 posts - 1 through 10 (of 10 total)

The topic ‘Post Object help’ is closed to new replies.