Skip to content

Commit

Permalink
New option "Related Meta Keys"
Browse files Browse the repository at this point in the history
Fixes #172
  • Loading branch information
ajaydsouza committed Nov 12, 2022
1 parent 8c385c3 commit 97a1e78
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 7 deletions.
8 changes: 8 additions & 0 deletions includes/admin/default-settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -457,6 +457,14 @@ function crp_settings_list() {
'options' => '1',
'min' => '1',
),
'related_meta_keys' => array(
'id' => 'related_meta_keys',
'name' => esc_html__( 'Related Meta Keys', 'contextual-related-posts' ),
'desc' => esc_html__( 'Enter a comma-separated list of meta keys. Posts that match the same value of the meta key are displayed before the other related posts', 'contextual-related-posts' ),
'type' => 'csv',
'options' => '',
'size' => 'large',
),
'exclude_post_ids' => array(
'id' => 'exclude_post_ids',
'name' => esc_html__( 'Post/page IDs to exclude', 'contextual-related-posts' ),
Expand Down
36 changes: 36 additions & 0 deletions includes/class-crp-query.php
Original file line number Diff line number Diff line change
Expand Up @@ -379,6 +379,8 @@ public function prepare_query_args( $args = array() ) {
$meta_query['relation'] = apply_filters( 'crp_query_meta_query_relation', 'AND', $args );
}

$args['meta_query'] = $meta_query; // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_query

// Set post_status.
$args['post_status'] = empty( $args['post_status'] ) ? array( 'publish', 'inherit' ) : $args['post_status'];

Expand Down Expand Up @@ -863,6 +865,40 @@ public function the_posts( $posts, $query ) {
shuffle( $posts );
}

// Related posts by meta_key.
if ( ! empty( $this->query_args['related_meta_keys'] ) ) {
$related_meta_query = array();
$related_meta_keys = wp_parse_list( $this->query_args['related_meta_keys'] );
foreach ( $related_meta_keys as $related_meta_key ) {
$related_meta_query[] = array(
'key' => $related_meta_key,
'value' => (string) get_post_meta( $this->source_post->ID, $related_meta_key, true ),
);
}

if ( count( $related_meta_query ) > 1 ) {
/**
* Filter the meta_query relation parameter for related posts by meta_key.
*
* @since 3.3.0
*
* @param string $relation The logical relationship between each inner meta_query array when there is more than one. Default is 'OR'.
*/
$related_meta_query['relation'] = apply_filters( 'crp_query_related_meta_query_relation', 'OR' );
}

$meta_posts = get_posts(
array(
'post__not_in' => (array) $this->source_post->ID,
'fields' => $query->get( 'fields' ),
'numberposts' => $query->get( 'posts_per_page' ),
'post_type' => $query->get( 'post_type' ),
'meta_query' => $related_meta_query, // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_query
)
);
$posts = array_merge( $meta_posts, $posts );
}

// Manual Posts (manual_related - set via the Post Meta) or Include Posts (can be set as a parameter).
$post_ids = array();

Expand Down
13 changes: 6 additions & 7 deletions includes/modules/shortcode.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,12 @@ function crp_shortcode( $atts, $content = null ) { // phpcs:ignore Generic.CodeA
array_merge(
$crp_settings,
array(
'heading' => 1,
'is_shortcode' => 1,
'offset' => 0,
'include_cat_ids' => '',
'include_post_ids' => '',
'heading' => 1,
'is_shortcode' => 1,
'offset' => 0,
'include_cat_ids' => '',
'include_post_ids' => '',
'related_meta_keys' => '',
)
),
$atts,
Expand All @@ -42,5 +43,3 @@ function crp_shortcode( $atts, $content = null ) { // phpcs:ignore Generic.CodeA
return get_crp( $atts );
}
add_shortcode( 'crp', 'crp_shortcode' );


1 change: 1 addition & 0 deletions readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,7 @@ You can insert the related posts anywhere in your post using the `[crp]` shortco

* Features:
* Related posts block allows you to set a custom header above the related posts. Leave blank to get the one from the main settings page.
* New option "Related Meta Keys" under the List Tuning tab. You can enter a comma-separted list of meta keys. Posts that match the same value of the meta key are displayed before the other related posts.

* Enhancements/modifications:
* If the number of "Manual related posts" is greater than the number of related posts, then the database query is bypassed drastically improving perfomance
Expand Down

0 comments on commit 97a1e78

Please sign in to comment.