Related_Posts
in package
Handle the common related posts functionality.
- Related post IDs are determined by shared categories and tags, with a preference for posts that share both.
- The related post IDs are stored as an array in the post metadata on the target post.
- Provide a method to bulk regenerate the related post IDs for all posts.
Tags
Table of Contents
Constants
- META_KEY = 'pnet_related_posts'
- Meta key name.
Properties
- $instances : array<string|int, object>
- The array of instances of this class and sub-classes.
- $max_related_posts : int
- The maximum number of related posts to return.
- $min_related_posts : int
- The minimum number of related posts to return.
Methods
- get_instance() : object
- Creates and/or returns the instance of this class.
- regenerate() : void
- Regenerate the list of related post IDs for all posts.
- set_ids() : array<string|int, int>
- Set the post metadata array of ids of the posts related to target post.
- __construct() : void
- Initialise the class.
- get_ids_loose() : array<string|int, int>
- Get list of post ids with any of the categories of target post.
- get_ids_moderate() : array<string|int, int>
- Get list of post ids with any of the categories and tags of target post.
- get_ids_strict() : array<string|int, int>
- Get list of post ids with identical categories and tags to target post.
Constants
META_KEY
Meta key name.
protected
string
META_KEY
= 'pnet_related_posts'
Tags
Properties
$instances
The array of instances of this class and sub-classes.
protected
static array<string|int, object>
$instances
= array()
Tags
$max_related_posts
The maximum number of related posts to return.
protected
int
$max_related_posts
= 0
Tags
$min_related_posts
The minimum number of related posts to return.
protected
int
$min_related_posts
= 0
Tags
Methods
get_instance()
Creates and/or returns the instance of this class.
public
static get_instance() : object
Tags
Return values
object —The instance of this class.
regenerate()
Regenerate the list of related post IDs for all posts.
public
regenerate() : void
- This method is called by a cron job to update the related posts list for all posts.
- This helps to ensure that the related posts list is relevant as categories and tags change.
Tags
set_ids()
Set the post metadata array of ids of the posts related to target post.
public
set_ids([int $post_id = 0 ]) : array<string|int, int>
- Related posts are determined by shared categories and tags, with a preference for posts that share both. If too few related posts are found, the criteria is gradually loosened to include posts that share only categories.
- The related post IDs are stored as an array in the post metadata on the target post.
- The array of related post IDs is returned to the calling function.
Parameters
- $post_id : int = 0
-
The id of the post to set related posts for.
Tags
Return values
array<string|int, int> —The array of related post IDs.
__construct()
Initialise the class.
protected
__construct() : void
Tags
get_ids_loose()
Get list of post ids with any of the categories of target post.
protected
get_ids_loose(int $post_id, string $post_type, array<string|int, int> $category_ids, array<string|int, int> $tag_ids, array<string|int, int> $excluded_posts) : array<string|int, int>
Parameters
- $post_id : int
-
The id of the post to retrieve related posts for.
- $post_type : string
-
The post type of the target post.
- $category_ids : array<string|int, int>
-
The category ids of the target post.
- $tag_ids : array<string|int, int>
-
The tag ids of the target post.
- $excluded_posts : array<string|int, int>
-
The post ids to exclude from the query.
Tags
Return values
array<string|int, int> —The array of related post IDs with identical categories.
get_ids_moderate()
Get list of post ids with any of the categories and tags of target post.
protected
get_ids_moderate(int $post_id, string $post_type, array<string|int, int> $category_ids, array<string|int, int> $tag_ids, array<string|int, int> $excluded_posts) : array<string|int, int>
Parameters
- $post_id : int
-
The id of the post to retrieve related posts for.
- $post_type : string
-
The post type of the target post.
- $category_ids : array<string|int, int>
-
The category ids of the target post.
- $tag_ids : array<string|int, int>
-
The tag ids of the target post.
- $excluded_posts : array<string|int, int>
-
The post ids to exclude from the query.
Tags
Return values
array<string|int, int> —The array of related post IDs with any of the target categories and tags.
get_ids_strict()
Get list of post ids with identical categories and tags to target post.
protected
get_ids_strict(int $post_id, string $post_type, array<string|int, int> $category_ids, array<string|int, int> $tag_ids, array<string|int, int> $excluded_posts) : array<string|int, int>
Parameters
- $post_id : int
-
The id of the post to retrieve related posts for.
- $post_type : string
-
The post type of the target post.
- $category_ids : array<string|int, int>
-
The category ids of the target post.
- $tag_ids : array<string|int, int>
-
The tag ids of the target post.
- $excluded_posts : array<string|int, int>
-
The post ids to exclude from the query.
Tags
Return values
array<string|int, int> —The array of related post IDs with identical categories and tags.