WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Seeking Advice (4 posts)

  1. Sarah_Frantz
    Member
    Posted 1 year ago #

    I am wondering if this is possible, and if so, I'm looking for advice on the best way to approach implementing it.

    I want to add extra fields to the comments form for users to leave ratings on three different things:

    • Price
    • Performance
    • Overall Rating

    I can somewhat modify the source from this tutorial on extending the comments form... but my issue/question is whether or not it is possible to organize the posts based on these extra comment meta fields.. For example, I want to be able to get all the posts from that particular category based on the score they received in the comment meta. Would I do a custom wp database query? Or is there an even easier way?

  2. tommcfarlin
    Member
    Posted 1 year ago #

    The short answer is that yes, it's possible to get all of the posts from the category based on that score (or any of the other meta data, for that matter), but you're very limited in your options when it comes to the API.

    To my knowledge - and I could be completely off base here - APIs such as WP_Query don't yet support querying data from the commentmeta table. If they do, it's not documented in the Codex article and I've not peeked at the source code to see if it's possible in 3.5+.

    Depending on how or where you want to filter the data, you may be able to use wp_list_comments and provide a custom callback function; however, if I understand your question correctly, it might be better off to write a query using $wpdb.

    If you go that route, make sure that you:

    • Use prepare to ensure you're not dealing with illegal input
    • Be very careful in how you SELECT and JOIN so not to pull back posts that are private, in draft, are auto-drafts, are revisions, etc.
    • And it sounds like you may be fine using get_results to retrieve what you're looking for.

    Finally, another way that you could make this faster (again, depending on your use case) is to come up with a query that just retrieves the post IDs that you need, then use WP_Query to do a proper retrieval of the posts.

    The bottom line is that it looks like you may have to do a custom query, but it may not have to be all or nothing.

    Consider what options you have and if you can leverage using $wpdb to pull back as little information as possible, pass the rest of the data to API and let it do its thing as it's more likely to optimally retrieve the data.

  3. Sarah_Frantz
    Member
    Posted 1 year ago #

    Thanks Tom! Your input is always much appreciated!

    I think I may go the $wpdb route (sounds like my cleanest/less painful option) - and then use your suggestion of doing it to just retrieve the Post ID. I think that might be a good method of trying to get what I need accomplished....

    Thanks again!

  4. Pioneer Valley Web Design
    Member
    Posted 1 year ago #

    I am intrigued at this discussion of a WP API!

Topic Closed

This topic has been closed to new replies.

About this Topic