The Site Object
To ensure you can use the function, make sure you followed the instructions on the initializing page. It’s literally one step, you can’t mess it up.
You’re free to rename the function if it clashes with any other names, but this documentation assumes you’ve instantiated the better-wordpress
class into a variable named $site
and it is referenced by a global function named site()
.
Calling the site()
function in any of your theme’s php files will return the ofc/Site
object, a class with a collection of convenient methods to interface with various parts of WordPress.
Patterns
Post Fields
One convenient feature of better-wordpress is the ability to define the structure of a returned post object on certain methods.
In implimentation all this means is creating a string array of WP_Post
members you want returned. For example, if I only want to get the $post_title
and $post_author
of a post, my fields would look like this:
$fields = ['post_title','post_author']
// Get these fields from post with id 4
$post = site()->getPost(4,$fields)
The result of getPost is the following:
[
"post_title" => "My Blog Post",
"post_author" => "Escher"
]
Additional Fields
In addition to any member of the WP_Post
object, better-wordpress comes with a collection of shortcuts for nested properties, meta fields, Advanced Custom Fields, and taxonomies.
Check out this getPost
example to see some of these fields in action.
id
– Shortcut to return the$ID
.title
– Shortcut to return the$post_title
.name
– Shortcut to return the$post_title
.url
– Usesget_permalink($id)
to fetch the url of the post.permalink
– Same asurl
.thumbnail
– Usesget_the_post_thumbnail_url($id)
to get the post thumbnail.content
– Retrieves the content of the post.excerpt
– Retrieves the post excerpt, if enabled.meta.<key>
– Get the value of a specific post meta field.acf.<key>
– Get the value of a specific ACF field for this post.categories.<member1>,<member2>,...
– Get specific members of theWP_Term
for each category this post is a part of.taxonomy.<taxonomy>.<member1>,<member2>,...
– Get specific members of theWP_Term
for each term within the given taxonomy this post is a part of. See below for members:term_id
id
– Alias forterm_id
link
– The result ofget_term_link()
name
title
– Alias forname
slug
description