• Hello, I want to order by the counter plugin my posts (custom post types), and I don’t find how to do it.

    In my loop I do this:

    $loop = new WP_Query(
        array(
            'post_type' => 'e',
            'posts_per_page' => 12,
            'paged' => $paged,
            'tax_query' => array(
                array(
                    'taxonomy' => 'c',
                    'field'    => 'slug', // term_id, slug  
                    'terms'    => $c
                ),
            )
        )
    );

    Where e is the custom post type slug and $c is the category.

    How do I join the two tables and be able to order it by the visit counter?

    Thanks in advance!

    The page I need help with: [log in to see the link]

Viewing 1 replies (of 1 total)
  • Thread Starter growmedia

    (@growmedia)

    I answer myself:

    add_filter('posts_clauses', 'join_and_order');
    function join_and_order($posts_clauses)
    {
    	global $wpdb;
    	// only change query on WooCommerce loops
    		$posts_clauses['join'] .= " left JOIN dh467t_puvc ON dh467t_puvc.puvc_id = dh467t_posts.ID";
    		$posts_clauses['orderby'] = " dh467t_puvc.puvc_view_count desc, " . $posts_clauses['orderby'];
    	
    	return $posts_clauses;
    }
Viewing 1 replies (of 1 total)
  • The topic ‘Order by counter’ is closed to new replies.