Created
September 23, 2014 15:51
-
-
Save bluvertigo/4b20c77d68e8442aba77 to your computer and use it in GitHub Desktop.
Shortcode - Order product / post by facebook like
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // 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'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment