• Resolved docnzok

    (@docnzok)


    I would like to create a sidebar widget with a custom query from the database. I have a field UserScore (a custom field) and want to retrieve the top 5 highest scores and the post title it relates to and place them in a sidebar.

    I have generated the correct query including the join from post_meta to posts. I just don’t know how to place it.

    I have tried a couple different plugins [Get Custom Field Values], [Query Posts] and [Widget Logic] but I just cannot get this to work.

    If I only have 1 sidebar, should I just place the query in there? I don’t know how to do that.

    MUCH APPRECIATED AS ALWAYS on this forum.

    Mark-Chicago

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

    (@docnzok)

    Occasionally, when you are really stuck, you start over again from square one and when you solve the problem, you feel really, really good.

    Here’s the solution:
    <?php $querystr = “
    SELECT * FROM $wpdb->posts as wpost
    INNER JOIN $wpdb->postmeta
    ON (wpost.ID = $wpdb->postmeta.post_id)
    AND $wpdb->postmeta.meta_key = ‘CustomField’
    ORDER BY $wpdb->postmeta.meta_value DESC
    LIMIT 5
    “;
    $pageposts = $wpdb->get_results($querystr, OBJECT); ?>

Viewing 1 replies (of 1 total)
  • The topic ‘Widget with Query’ is closed to new replies.