|
<?php |
|
/** |
|
* Related Posts via BeaverBuilder/Metaboxi.io |
|
* BB Posts Module + Relationship Field |
|
*/ |
|
class PXB_Related_Posts_BBMB { |
|
/** |
|
* ID of target posts module (Beaver Builder) |
|
*/ |
|
private $module_id = 'related-posts'; |
|
/** |
|
* ID of relationship metabox |
|
*/ |
|
private $metabox_id = 'posts_to_posts'; |
|
/** |
|
* Init |
|
*/ |
|
public function init() { |
|
if( ! $this->meets_requirements() ) { |
|
return; |
|
} |
|
// add relationship field |
|
add_action( 'mb_relationships_init', [ $this, 'create_mb_related' ] ); |
|
// frontend: filter bb posts module |
|
if( ! is_admin() ) { |
|
add_filter( 'fl_builder_loop_query_args', [ $this, 'filter_bb_posts_args' ] ); |
|
} |
|
} |
|
/** |
|
* Check Requirements |
|
*/ |
|
public function meets_requirements() { |
|
if( ! class_exists( 'RW_Meta_Box' ) ) { |
|
return false; |
|
} |
|
if( ! class_exists( 'FLBuilder' ) ) { |
|
return false; |
|
} |
|
return true; |
|
} |
|
/** |
|
* Create MB Relationship (posts_to_posts) |
|
*/ |
|
public function create_mb_related() { |
|
MB_Relationships_API::register( array( |
|
'id' => $this->metabox_id, |
|
'from' => 'post', |
|
'to' => 'post', |
|
) ); |
|
} |
|
/** |
|
* Filter the Beaver Builder posts module |
|
* shows related posts via metaboxio post relationship |
|
* |
|
* @link https://kb.wpbeaverbuilder.com/article/591-create-a-filter-to-customize-the-display-of-post-data |
|
*/ |
|
public function filter_bb_posts_args( $query_args ) { |
|
// only apply this if module has the required id |
|
if ( $this->module_id === $query_args['settings']->id ) { |
|
// get ids linked by relationship |
|
$related_ids = $this->mb_beaver_related_posts(); |
|
// change the BB query if ids were found |
|
if( $related_ids && is_array( $related_ids ) ) { |
|
$query_args['post__in'] = $related_ids; |
|
} |
|
} |
|
return $query_args; |
|
} |
|
/** |
|
* Helper function - get related post ids |
|
* @link https://docs.metabox.io/extensions/mb-relationships/ |
|
*/ |
|
function mb_beaver_related_posts() { |
|
if( ! $post_id = get_the_ID() ) { |
|
return false; |
|
} |
|
|
|
// @TODO: cache these ids into single post_meta (to avoid extra query when possible). It would need to update all related post_meta on post_save. |
|
|
|
// Query args - get metabox relationship field ids |
|
$args = array( |
|
'relationship' => array( |
|
'id' => $this->metabox_id, |
|
'from' => $post_id, |
|
), |
|
// make WP_Query more efficient |
|
'no_found_rows' => true, |
|
'update_post_meta_cache' => false, |
|
'update_post_term_cache' => false, |
|
'fields' => 'ids' |
|
); |
|
$connected = new WP_Query( $args ); |
|
if( isset( $connected->posts ) && is_array( $connected->posts ) ) { |
|
return $connected->posts; |
|
} |
|
return false; |
|
} |
|
|
|
} // end: PXB_Related_Posts_BBMB |
|
|
|
// Load class |
|
function load_pxb_related_posts_bbmb() { |
|
( new PXB_Related_Posts_BBMB() )->init(); |
|
} |
|
add_action( 'plugins_loaded', 'load_pxb_related_posts_bbmb', 11 ); |