Mediavine Create – Recipe Rating

/**
 * Entry Rating
 *
 */
function be_entry_rating() {

	if( ! ( class_exists( 'Mediavine\MV_DBI' ) ) )
		return;

	/**
	 * There's currently a bug where some of the creations are storing associated_posts
	 * with quotes and some without. Ex: [123] and ["456"].
	 * mv_get_post_creations() only finds the quoted ones, which is why we're
	 * doing the complex query below.
	 *
	 * Once that bug is resolved, the code below can be simplified to:
	 * https://gist.github.com/billerickson/8c69e26a7e6a0b1efdce9c30ad680adf
	 *
	 */

	$creations = \Mediavine\MV_DBI::get_models(['mv_creations'])->mv_creations->where( [
	 	['associated_posts', 'LIKE', '%"' . get_the_ID() . '"%', 'or'],
		['associated_posts', 'LIKE', '%[' . get_the_ID() . ']%' ]]
	);

	if( empty( $creations ) || ! is_array( $creations ) || empty( $creations[0]->rating ) )
		return;

	echo '<span class="entry-rating">' . str_repeat( '★', round( $creations[0]->rating ) ) . '</span>';
}
Bill Erickson

Bill Erickson is the co-founder and lead developer at CultivateWP, a WordPress agency focusing on high performance sites for web publishers.

About Me
Ready to upgrade your website?

I build custom WordPress websites that look great and are easy to manage.

Let's Talk