create a favorites plugin with likes for posts, and create a results page based on higher number

Home » Snippets » create a favorites plugin with likes for posts, and create a results page based on higher number

Created with:




Customize with WPTurbo AI

Add Snippet To Project

New Project
Add To Existing Project
Plugin Name: Favorites Plugin
Plugin URI:
Description: Adds a favorites functionality to posts with likes.
Version: 1.0
Author: WP Turbo
Author URI:
License: GPL2
Text Domain: wpturbo

// Register the Favorites post meta field
function wpturbo_add_favorites_meta_field() {
    register_meta('post', 'favorites', array(
        'type' => 'integer',
        'single' => true,
        'show_in_rest' => true,
add_action('init', 'wpturbo_add_favorites_meta_field');

// Enqueue the necessary scripts and styles
function wpturbo_enqueue_scripts() {
    if (is_single()) {
        wp_enqueue_script('wpturbo-likes-script', plugin_dir_url(__FILE__) . 'js/likes.js', array('jquery'), '1.0', true);
        wp_localize_script('wpturbo-likes-script', 'wpturbo_likes_params', array(
            'ajax_url' => admin_url('admin-ajax.php'),
            'nonce' => wp_create_nonce('wpturbo-likes-nonce')
        wp_enqueue_style('wpturbo-likes-style', plugin_dir_url(__FILE__) . 'css/likes.css', array(), '1.0');
add_action('wp_enqueue_scripts', 'wpturbo_enqueue_scripts');

// Handle the AJAX request to increment the post likes
function wpturbo_increment_likes() {
    if (!isset($_POST['post_id']) || !isset($_POST['nonce']) || !wp_verify_nonce($_POST['nonce'], 'wpturbo-likes-nonce')) {
        wp_send_json_error('Invalid request');

    $post_id = intval($_POST['post_id']);
    $likes = get_post_meta($post_id, 'favorites', true);
    $likes = $likes ? $likes + 1 : 1;
    update_post_meta($post_id, 'favorites', $likes);

add_action('wp_ajax_wpturbo_increment_likes', 'wpturbo_increment_likes');
add_action('wp_ajax_nopriv_wpturbo_increment_likes', 'wpturbo_increment_likes');

// Create a shortcode to display the favorites count
function wpturbo_show_favorites_count($atts) {
    $atts = shortcode_atts(array(
        'post_id' => get_the_ID()
    ), $atts);

    $post_id = intval($atts['post_id']);
    $likes = get_post_meta($post_id, 'favorites', true);

    return '<span class="wpturbo-favorites-count">' . $likes . '</span>';
add_shortcode('favorites_count', 'wpturbo_show_favorites_count');

// Create a function to sort posts by favorites count
function wpturbo_sort_posts_by_favorites($query) {
    if ($query->is_main_query() && $query->is_post_type_archive('post')) {
        $query->set('orderby', 'meta_value_num');
        $query->set('meta_key', 'favorites');
        $query->set('order', 'DESC');
add_action('pre_get_posts', 'wpturbo_sort_posts_by_favorites');

Let me explain the changes I made:

You can further customize the plugin by creating a template for the results page where you display the posts sorted by the number of likes using the favorites_count shortcode.

Please note that this code assumes you have separate JavaScript and CSS files located in the js and css directories respectively. You may need to create these directories and add the respective files.

I hope this helps you create your favorites plugin with likes for posts and a results page based on the higher number of likes. Let me know if you have any further questions!

Register an account to save your snippets or go Pro to get more features.