• Howdy.

    I am attempting to use WordPress for a digg-style website where users rank videos.

    I have added the variable “zaprank” to the wp_posts table; this is a 1-10 used to rank each video, held as FLOAT.

    I can easily access my var with $post->zaprank, so I know it is in there and working properly.

    On my home page, I want to ORDER BY zaprank DESC, so that the top-rated stuff is on top, just like digg, reddit, etc.

    Here are my results:

    https://zaphodforpresident.com/projectx/

    Here is my code:

    <?php $posts = query_posts($query_string.’&orderby=zapscore&orderby=DESC’); ?>

    In MySQL I want this:

    SELECT * FROM wp_posts WHERE 1 ORDER BY zapscore DESC

    I also want to include any pagination (i.e. LIMIT 10, 1)

    Any thoughts?

Viewing 4 replies - 1 through 4 (of 4 total)
  • Check the code in the query.php page in INCLUDES. I think it parses the query_posts value, so “orderby=zapscore” won’t work.

    there are a couple of plugin hooks (actions) you can use to change the query_posts() sql query, like:

    • posts_where
    • posts_join
    • posts_orderby
    • posts_request

    Using this you can tap into the “sql query” and modify it. For details do open the wp-includes/query.php and explore it to see where the hooks are applied.

    Moderator Samuel Wood (Otto)

    (@otto42)

    www.remarpro.com Admin

    function my_custom_orderby($old) {
     // $old has what order by was before
     return 'zapscore DESC'; // the query auto-adds "ORDER BY" for us.
    }
    
    if (is_home()) { // we only do this on the home page, yes?
    add_filter('posts_orderby', 'my_custom_orderby');
    }
    Thread Starter zaphod2016

    (@zaphod2016)

    Kaloyan, Otto42- THANK YOU!

    This is exactly what I was looking for.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘WP hacks; customize output with query_posts’ is closed to new replies.