pfeifferNet

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
since
6.7.0
used-by
Related_Posts::__construct()
used-by
Related_Posts::__construct()

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
since
6.7.0

Properties

$instances

The array of instances of this class and sub-classes.

protected static array<string|int, object> $instances = array()
Tags
since
6.7.0

The maximum number of related posts to return.

protected int $max_related_posts = 0
Tags
since
6.7.0

The minimum number of related posts to return.

protected int $min_related_posts = 0
Tags
since
6.7.0

Methods

get_instance()

Creates and/or returns the instance of this class.

public static get_instance() : object
Tags
since
6.7.0
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
since
6.7.1

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
since
6.7.0
used-by
Related_Posts::handle_ajax_request()
used-by
Related_Posts::get_ids()
Return values
array<string|int, int>

The array of related post IDs.

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
since
6.7.0
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
since
6.7.0
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
since
6.7.0
Return values
array<string|int, int>

The array of related post IDs with identical categories and tags.


        
On this page

Search results