Search by

arraypress / wp-object-utils

arraypress

Find a post, term or user by whatever you were handed: an id, a slug, a name, an email or the object itself.

Package info

github.com/arraypress/wp-object-utils

Homepage

pkg:composer/arraypress/wp-object-utils

Statistics

Installs: 11

Dependents: 0

Suggesters: 0

Stars: 0

Open Issues: 0

v1.0.0 2026-08-25 18:41 UTC

This package is auto-updated.

Last update: 2026-09-26 09:03:23 UTC


README

Find a post, term or user by whatever you were handed: an id, a slug, a name, an email, or the object itself.

What it does

Core has a different function per field — get_page_by_path(), get_term_by(), get_user_by() — so before you can look something up you have to work out what you are holding. Code that accepts input from a shortcode, a REST request or a CSV import ends up doing that by hand, usually with a chain of is_numeric() checks.

This takes any of them and gives you the object back, or null.

Features

  • Resolve a post, term or user from an id, slug, name, title or email
  • Pass the object itself and get it straight back, so callers need no special case
  • Turn a list of mixed identifiers into objects in one call
  • Ask only for the id, when that is all you need
  • Check something exists without loading it
  • Search by a partial string, ready for a select or an autocomplete

Installation

composer require arraypress/wp-object-utils

Quick start

A shortcode that takes whatever the author typed:

use ArrayPress\ObjectUtils\Posts;

add_shortcode( 'product_price', function ( $atts ) {
	// 'id', 'my-product-slug' or 'My Product' all work.
	$product = Posts::find( $atts['product'] ?? 0, 'product' );

	if ( ! $product ) {
		return '';
	}

	return esc_html( get_post_meta( $product->ID, '_price', true ) );
} );

Users::find() takes just the identifier — id, login, email, slug or the WP_User itself. Terms::find() takes the taxonomy as its second argument, since a slug is only unique within one.

Requirements

  • PHP 8.3 or later
  • WordPress 7.1 or later

License

GPL-2.0-or-later