WPLake > Learning Hub > ACF Post Object Field

ACF Post Object Field

Delve into ACF's Post Object field versus Relationship, bidirectional support, storage methods, and diverse value formats for efficient content management.

Key Points at a Glance

  1. Similarities and Differences with Relationship Fields: The ACF plugin offers both Relationship and Post Object fields, each tailored for establishing content relationships, but with distinct features and use cases.
  2. Bidirectional Feature Support: Like Relationship fields, Post Object fields support bidirectional linking, automating field updates on related objects, and eliminating manual queries.
  3. Storage of Values as IDs: Post Object fields consistently store selected posts as IDs, regardless of Return Format settings, simplifying database queries but requiring consideration during manual queries.
  4. Four Different Value Formats: The get_field function may return arrays of WP_Post instances or post IDs, depending on field settings, offering flexibility but requiring attention to specific settings.

Table of Contents

About the ACF Post Object field

Advanced Custom Fields (ACF) is one of the leading meta field plugins. The Post object field is part of the Relationship group fields, allowing editors to select one or more posts or custom post type (CPT) items. It can be viewed as an extension of the Select field specifically tailored for post objects.

With this field, you can 'relate' one post to others, such as a Company referring to Employees' CPT, and vice versa. It achieves the same goal as the Relationship field but has several differences, which we'll explore below.

ACF Post Object field
Settings of the Post Object field

In addition to the classical field settings such as label and name, the Post Object field includes three filter fields: Post Type, Post Status, and Taxonomy. These filters enable fine-tuning of the available options for editors.

The Advanced tab conceals the bidirectional feature, which we will examine in detail below.

The ACF Post Object field looks like a select. Only the search box by post titles is available for filtering.
The look of the Post Object field for editors

Now let's examine the key aspects of the Post Object field to gain insight into its functionality.

1. Post Object field vs Relationship field

The Advanced Custom Fields plugin offers two robust field types for establishing relationships between different content items: the Relationship field and the Post Object field. While both fields serve similar purposes, they possess distinct characteristics tailored to various use cases.

In this chapter, we will delve into the disparities between these two field types to assist you in determining which one aligns best with your requirements.

Relationship field

The Relationship field is specifically engineered to forge associations between posts of different post types. It facilitates many-to-many relationships, allowing a single post to link to multiple related posts, and vice versa.

This field proves invaluable for scenarios necessitating the showcasing of related content, the establishment of custom post relationships, or the creation of a network of interconnected content.

ACF Relationship field. Editors can easily find and attach the target posts using the search input and filters.
The look of the Relationship field for editors

Key Features of the Relationship Field:

  • Facilitates many-to-many relationships between different post types.
  • Enables bidirectional linking of posts.
  • Ideal for constructing custom post relationships and interconnected content.
  • Offers an intuitive interface for searching, selecting, and arranging related posts.

Post Object field

Conversely, the Post Object Field fulfills similar needs but presents a simpler interface without filters. It may prove useful if you prefer not to provide filtering options for editors. Additionally, by default, the field permits the selection of only a single item, resembling the traditional "foreign key" concept in databases.

The ACF Post Object field resembles a select, offering solely a search box by post titles for filtering.

The ACF Post Object field looks like a select. Only the search box by post titles is available for filtering.
The look of the Post Object field for editors

Key Features of the Post Object Field:

  • Simplified UI.
  • Absence of filters for editors.
  • Suited for a single relationship.

Choosing the Right Field Type

The decision to employ either the Relationship field or the Post Object field hinges on the nature of your content and the specific project requirements.

Use the Relationship field when:

  • Many-to-many relationships between different post types are necessary.
  • Your content structure entails interconnected or related posts requiring bidirectional linking.
  • You seek a user-friendly interface for effortless searching, selecting, and managing related posts.

Use the Post Object field when:

  • Only a single post needs association with another post, page, or custom post type.
  • Relationships between your content items are straightforward and do not necessitate bidirectional linking.

In conclusion, both the Relationship field and the Post Object field serve as potent tools for establishing relationships between content items in WordPress.

By comprehending their disparities and understanding your project's specific requirements, you can confidently select the field type that best suits your needs, thereby fostering a seamless and efficient content management experience.

2. Bidirectional feature of the Post Object field

The same as the Relationship field, the ACF Post Object field supports the bidirectional feature, which automates the updating of fields on related objects. This functionality eliminates the need for manual queries and simplifies the use of field values, particularly for beginners.

With the bidirectional setting enabled for the ACF Post Object field, related fields are automatically kept up-to-date.

So, how does it function?

Let's illustrate this with an example involving Company and Employee custom post types (CPTs). A company can have multiple employees, which is represented by an 'Employees' Post Object field in the Company CPT. This aspect is relatively straightforward.

Now, let's delve into the task at hand. Our goal is to retrieve a list of all companies where the current employee is associated. With the introduction of the Bidirectional feature, we can accomplish this without creating a custom WP_Query. Here's how:

  1. Create a New Post Object Field:
    Add another Post Object field, perhaps named 'Companies', to the Employee CPT.
  2. Enable the Bidirectional feature:
    Navigate to the 'Employees' Post Object field and enable the Bidirectional feature. Set the target field as the new 'Companies' field.

Once these steps are completed, ACF will automatically track changes. When you update the 'Employees' Post Object field, ACF will automatically update the related company in the associated employee's 'Companies' field.

This approach ensures that you always obtain an up-to-date list of the companies associated with each employee, just like the value of the original Post Object field.

Pro tip: You can further enhance this setup by enabling the Bidirectional feature for the 'Companies' Post Object field as well. This way, any additions or removals of companies on the employee's side will be automatically reflected on the company's side too.

ACF Company Group with the Bidirectional Employees Relationship
Company Group with Bidirectional Employees. ACF will automatically add or remove the current Company from the target field when you add or remove an Employee.
ACF Company Group with the Bidirectional Employees Relationship
Employee Group with Bidirectional Companies. ACF will automatically add or remove the current Employee from the target field when you add or remove a Company.

3. Database value format of the ACF Post Object field

It's crucial to keep in mind that the Post Object field consistently stores selected posts as IDs, irrespective of the Return Format settings. If the multiple option is disabled, a single ID will be stored. However, if it's enabled, even if only a single item is selected, a serialized array of IDs will be stored.

This is something to consider when you need to proficiently handle manual database queries.

To streamline querying by meta fields, you can leverage the Advanced Views Pro plugin. It introduces smart templates with integrated post queries. The query feature operates using WP_Query in the background and supports ACF fields, alleviating concerns about handling formats.

4. Value formats of the Post Object field

While IDs are stored in the database, the get_field function may yield varying responses for the Post Object field in code, depending on the field settings. These responses can include:

  • An array of WP_Post instances
  • An array of post IDs
  • A single WP_Post instance
  • A single post ID

The 'multiple' setting determines whether the return value will be an array or a single item, while the 'Return format' setting dictates the value type. If you've enabled the 'multiple' setting, it will always return an array, regardless of whether the single item option is checked.

The extensive field settings offer flexibility to tailor setups according to our requirements. However, they can also complicate coding tasks, as we must consistently remember the specific field settings.

To circumvent this challenge, consider leveraging smart templates from the Advanced Views Lite plugin.

These templates introduce automated generation and built-in post queries. They handle tasks such as retrieving field values, converting formats, and passing them to the template. This allows you to concentrate on designing the layout without worrying about the intricacies of field settings.

Summary

The Post object field facilitates the relationship between post objects and custom post type (CPT) items. With the 'multiple' setting, you can establish one-to-one, one-to-many, and many-to-many relations according to your requirements.

Functionally, the field operates similarly to the Relationship field, with the primary distinction lying in the UI for editors.

Enabling the Bidirectional feature transforms the Post object field into a superfield, ensuring the related field remains up-to-date and eliminating the need for manual database queries.

In terms of database storage, regardless of the field settings, it stores post IDs or a serialized array of IDs. To streamline query tasks and avoid complications, consider utilizing Advanced Views Pro, which simplifies query management.

When working with this field in templates, it's essential to recall the field settings to determine which of the four value formats will be returned by the get_field function.

For simplified handling, leverage smart templates from the Advanced Views Lite plugin, which automates tasks such as retrieving field values, format conversion, and template integration.

Frequently Asked Questions

  1. What is the difference between the Relationship field and the Post Object field in ACF?

    The Relationship field enables many-to-many relationships between different post types with bidirectional linking, while the Post Object field simplifies UI without filters, suitable for single relationships.

  2. When should I choose the Relationship field over the Post Object field?

    Choose the Relationship field for many-to-many relationships or bidirectional linking needs, while the Post Object field suits single relationships and straightforward content structures.

  3. How does the bidirectional feature work in ACF Post Object fields?

    The bidirectional feature automatically updates fields on related objects, eliminating manual queries, and ensuring field values remain up-to-date, similar to Relationship fields.

Content links (19)

Related articles

About the Author

Maxim Akimov

Certified WordPress expert from Ukraine with over 8 years of experience. Advocate of the BEM methodology and the overall modular approach. Loves sporting activities and enjoys going to the gym and regularly plays table tennis.

0 Comments

    Leave a comment

    Reply to 

    Please be considerate when leaving a comment.

    Not shown publicly

    Got it