• Resolved supertester

    (@supertester)


    Hi,

    When using ‘v_orderby’ and ‘v_sortby’ in combination with ‘meta_query’, the query breaks.

    Check out the following WP Query

    $args = array(
    	'v_sortby' => 'views',
    	'v_orderby' => 'DESC',
    	'showposts' => 20,
    	'meta_query' => array(
                array(
    	       'key' => 'somekey',
    	       'value' => 'somevalue'
    	    );
    	)
    );
    $go = new WP_Query( $args );

    This gives the following MySQL query:
    SELECT SQL_CALC_FOUND_ROWS wp_posts.*, (wp_postmeta.meta_value+0) AS views FROM wp_posts INNER JOIN wp_postmeta ON (wp_posts.ID = wp_postmeta.post_id) LEFT JOIN wp_postmeta ON wp_postmeta.post_id = wp_posts.ID WHERE 1=1 AND ( (wp_postmeta.meta_key = 'somekey' AND CAST(wp_postmeta.meta_value AS CHAR) = 'somevalue') ) AND wp_postmeta.meta_key = 'views' GROUP BY wp_posts.ID ORDER BY views desc LIMIT 0, 20

    And that query ofcourse gives an error: Not unique table/alias: ‘wp_postmeta’

    Any idea how to fix this?

    http://wordpress.org/plugins/wp-postviews/

Viewing 2 replies - 1 through 2 (of 2 total)
  • Plugin Author Lester Chan

    (@gamerz)

    Before the scenes, post views is actually storing it in the meta table. You might now want to use v_* but instead just use views and put it in the custom meta field.

    Check out http://codex.wordpress.org/Class_Reference/WP_Meta_Query in multiple meta entries

    Thread Starter supertester

    (@supertester)

    Yes I already found that fix but couldn’t find the topic to set it on ‘resolved’. I fixed it with the following code, in case anyone else needs it:

    $args = array(
    	'meta_key' => 'views',
    	'orderby' => 'meta_value_num',
    	'order' => 'DESC',
    	'meta_query' => array(
    		array(
    	            'key' => 'otherkey',
    	            'value' => 'othervalue'
    	         );
    	)
Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Query breaks when using v_orderby and meta_query’ is closed to new replies.