On this page
- Category: Field Types
- Written: January 4, 2012
- Updated: July 6, 2012
Post Object
The post object field creates a select field where the choices are your pages + posts + custom post types. This field is useful for advanced linking to another page / post.
Creating a post object field
The post object field contains options to customize your field:
- Post type: You can filter the choices by selecting post types.
- Filter from Taxonomy: You can filter the choices even more by selecting specific taxonomies / categories
- Multiple: By ticking the multiple option, the select field will now allow multiple selections and the API will return an array instead of a single value.
- Allow Null?: If selected, the select list will begin with a null value titled “- Select -”
Edit screen
Template usage
The API will return either a single post object (API uses get_post) or an array of post objects (API uses get_posts).
<?php /* * View array data (for debugging) */ var_dump( get_field('post_objects') ); /* * Loop through post objects (assuming this is a multi-select field) ( setup postdata ) * Using this method, you can use all the normal WP functions as the $post object is temporarily initialized within the loop * Read more: http://codex.wordpress.org/Template_Tags/get_posts#Reset_after_Postlists_with_offset */ $post_objects = get_field('post_objects'); if( $post_objects ): ?> <ul> <?php foreach( $post_objects as $post): // variable must be called $post (IMPORTANT) ?> <?php setup_postdata($post); ?> <li> <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a> <span>Post Object Custom Field: <?php the_field('field_name'); ?></span> </li> <?php endforeach; ?> </ul> <?php wp_reset_postdata(); // IMPORTANT - reset the $post object so the rest of the page works correctly ?> <?php endif; /* * Loop through post objects (assuming this is a multi-select field) ( don't setup postdata ) * Using this method, the $post object is never changed so all functions need a seccond parameter of the post ID in question. */ $post_objects = get_field('post_objects'); if( $post_objects ): ?> <ul> <?php foreach( $post_objects as $post_object): ?> <li> <a href="<?php echo get_permalink($post_object->ID); ?>"><?php echo get_the_title($post_object->ID); ?></a> <span>Post Object Custom Field: <?php the_field('field_name', $post_object->ID); ?></span> </li> <?php endforeach; ?> </ul> <?php endif; ?>




