WordPress.org

Ready to get started?Download WordPress

Forums

How to sort posts by post meta in WordPress (6 posts)

  1. A7bert
    Member
    Posted 4 months ago #

    Hi experts,

    I am new to WordPress, I got a site which is using the "I love it!" theme. It provides a like function and record number of likes.

    I am trying to sort all the posts by the number of likes, but I just don't know how to do it. I have tried plugins as well, but the meta key of each post in that theme are being stored in a different table "postmeta".

    query = new WP_Query( array ( 'orderby' => 'meta_value_num', 'meta_key' => 'nr_like' ) );

    I can't find the loop in the theme, I have made up the query script, I don't know where should I put it into to make it work.

    Could anyone help me or just advice me, pretty confused actually?

  2. Marvel Labs
    Member
    Posted 4 months ago #

  3. A7bert
    Member
    Posted 4 months ago #

    Looks like it doesn't support display the number of likes in the post list.

  4. A7bert
    Member
    Posted 4 months ago #

    Anyone can help me?

  5. Evan Herman
    Member
    Posted 4 months ago #

    Hey A7bert. I just went through a very intensive query based project.

    I had to set up an events posting section, where each post had a start and end date and then I had to query all posts that fell within the correct timeframe.

    Take a look at tax_query:

    $args = array(
       'meta_key' => 'nr_like',
       'order' => 'ASC',
       'meta_query' => array(
           array(
               'key' => 'nr_like'
           )
       ),
       'orderby' => 'meta_value_num'
     );
     $query = new WP_Query($args);

    Evan

  6. A7bert
    Member
    Posted 3 months ago #

    Oh no, sorry for my late reply, I thought nobody was looking at this anymore.

    Thank you so much, Evan, your query is exactly what I need, but my theme is just so strange... I have written the query, but I really don't know where to put, I don't understand its loop.

    Also, I found the number of likes is stored in the postmeta's meta_key column. Is it possible to create some sql script to make the value of the meta key as the post's custom field?

    I know I have asked a bit much and complicated. Thanks in advance, I will update this post with the result.

Reply

You must log in to post.

About this Topic