Skip to content

WP Objects

Radoslav Georgiev edited this page Oct 21, 2018 · 3 revisions

Purpose

The WP Objects field extends the WP Object field by allowing editors to select more than one object.

Play Preview Video

Options

The WP Objects field builds on top of the WP Object field, inheriting all of its options:

Maximum amount of items

You can adjust the maximum amount of items, which can be added to the field.

In PHP, use the set_max( $max ) method of the field:

Field::create( 'wp_objects', 'related_posts' )->set_max( 5 )

Output Format

In addition to the Output Type setting, which defines how to handle every individual item, the Objects field also has the "Output Format" setting:

  • Separate the values with commas (comma)
  • Ordered list (ordered)
  • Unordered list (unordered)
  • Paragraphs (paragraphs)

In PHP, use the set_output_format( $format ) method:

Field::create( 'wp_objects', 'related_articles' )->set_output_format( 'commas' );

Usage

<!-- Output a list of related articles -->
<?php the_value( 'related_articles' ) ?>

<!-- Use the list of articles manually -->
<?php
$links = array();

foreach( get_value( 'related_pages' ) as $item ) {
    if( is_a( $item, 'WP_Post' ) ) {
        $links[] = get_permalink( $item->ID );
    } elseif( is_a( $item, 'WP_Term' ) ) {
        $links[] = get_term_link( $item->term_id, $item->taxonomy );
    } elseif( is_a( $item, 'WP_User' ) ) {
        $links[] = get_author_link( false, $item->ID );
    }    
}
Clone this wiki locally