bluvertigo
9/23/2014 - 3:51 PM

Shortcode - Order product / post by facebook like

Shortcode - Order product / post by facebook like

// Incremente meta_value 'facebook_like_count'
function update_facebook_like_count( $post_id  ) {
	$string = file_get_contents( "http://graph.facebook.com/?id=".get_permalink( $post_id ) );
	$json=json_decode($string,true);
	update_post_meta( $post_id , 'facebook_like_count', $json['shares']);
}

// Shortcode per la visualizzazione degli articoli ordinati per numero di like
function products_order_facebook_like($atts) {
    global $woocommerce_loop, $woocommerce;

    extract(shortcode_atts(array(
        'per_page'  => '12',
        'columns'   => '4'
    ), $atts));

    $meta_query = $woocommerce->query->get_meta_query();

    $args = array(
        'post_type' => 'product',
        'post_status' => 'publish',
        'meta_key' => 'facebook_like_count',
		'orderby' => 'meta_value_num',
        'posts_per_page' => $per_page,
        'order' => 'desc',
        'meta_query' => $meta_query
    );

    ob_start();

    $products = new WP_Query( $args );

    $woocommerce_loop['columns'] = $columns;

    if ( $products->have_posts() ) : ?>

        <?php woocommerce_product_loop_start(); ?>

            <?php while ( $products->have_posts() ) : $products->the_post(); ?>

                <?php woocommerce_get_template_part( 'content', 'product' ); ?>

            <?php endwhile; // end of the loop. ?>

        <?php woocommerce_product_loop_end(); ?>

    <?php endif;

    wp_reset_postdata();

    return '<div class="MY_CUSTOM_CLASS">' . ob_get_clean() . '</div>';
 }
 add_shortcode('facebook_like_product','products_order_facebook_like');