Paginate Wordpress $wpdb Query?

后端 未结 4 1903
说谎
说谎 2021-01-16 14:38

I have this query:

    posts wposts, $wpdb->postmeta wpostmeta WHERE wposts.ID = wpostmeta.post_id A         


        
4条回答
  •  青春惊慌失措
    2021-01-16 14:43

    Although I wouldn't recommend it, you could try changing the properties of the global $wp_query object.

    global $wp_query; // shouldn't be required
    $query = "SELECT wposts.* FROM $wpdb->posts wposts, $wpdb->postmeta wpostmeta 
       WHERE wposts.ID = wpostmeta.post_id AND wpostmeta.meta_key = 'votes'
       AND wposts.post_status = 'publish' AND wposts.post_type = 'post' ORDER BY 
       CAST(wpostmeta.meta_value AS SIGNED) DESC LIMIT 10";
    $posts = $wpdb->get_results($query, OBJECT);
    
    $wp_query->posts = $posts;
    $wp_query->is_paged = true;
    $wp_query->current_post = -1;
    // etc etc
    

    You can look up the definition of the WP_Query class or do a var_dump() or print_r() on the $wp_query object after calling query_posts.

    Good luck!

提交回复
热议问题