Support » Plugin: WordPress Popular Posts » How To: Sort a custom query by views with WPML installed

  • Previous topic “How To: Sort a custom query by views (All time, monthly, weekly, or daily)” doesn’t work for me, because when I run custom query, it updates only original post, but other WPML language variations of that post don’t have this meta, so I need to rewrite it a little bit – maybe it will help someone 😉

    It works only when you visit the translated post.

    add_action( 'wpp_post_update_views', 'custom_wpp_update_postviews' );
    function custom_wpp_update_postviews( $post_id ){
    	if( function_exists('wpp_get_views') ){
    		update_post_meta( $post_id, 'views_total', wpp_get_views( $post_id, 'all', false ) );
    		update_post_meta( $post_id, 'views_daily', wpp_get_views( $post_id, 'daily', false ) );
    		update_post_meta( $post_id, 'views_weekly', wpp_get_views( $post_id, 'weekly', false ) );
    		update_post_meta( $post_id, 'views_monthly', wpp_get_views( $post_id, 'monthly', false ) );
    		if( isset( $_POST['wpp_id'] ) && $_POST['wpp_id'] != $post_id ){
    			$translated_post_id = (int)$_POST['wpp_id'];
    			update_post_meta( $translated_post_id, 'views_total', wpp_get_views( $post_id, 'all', false ) );
    			update_post_meta( $translated_post_id, 'views_daily', wpp_get_views( $post_id, 'daily', false ) );
    			update_post_meta( $translated_post_id, 'views_weekly', wpp_get_views( $post_id, 'weekly', false ) );
    			update_post_meta( $translated_post_id, 'views_monthly', wpp_get_views( $post_id, 'monthly', false ) );
    		}
    	}
    }
  • You must be logged in to reply to this topic.