WordPress.org

Ready to get started?Download WordPress

Forums

WP-PostViews
[resolved] Query breaks when using v_orderby and meta_query (3 posts)

  1. supertester
    Member
    Posted 11 months ago #

    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/

  2. Lester Chan
    Member
    Plugin Author

    Posted 11 months ago #

    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

  3. supertester
    Member
    Posted 11 months ago #

    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'
    	         );
    	)

Reply

You must log in to post.

About this Plugin

About this Topic

Tags

No tags yet.