WordPress.org

Ready to get started?Download WordPress

Forums

Custom select query - sort by meta value within category (9 posts)

  1. LesleyT
    Member
    Posted 6 years ago #

    Hi, I've tried all sorts of ways to make this work, and have investigated options in http://codex.wordpress.org/Displaying_Posts_Using_a_Custom_Select_Query and all over the forum, but am failing.

    What I'm after is when a user clicks on a category in the sidebar, they are taken to category.php which displays posts only in that category and then sorted by meta value. I've got the meta_value ASC bit working, but currently category.php shows all posts in all categories.
    this is my code so far - can anyone tell me how I also include the info that returns only the posts included in the category chosen in the sidebar.

    $querystr = "
    SELECT wposts.*
    FROM $wpdb->posts wposts, $wpdb->postmeta wpostmeta
    WHERE wposts.ID = wpostmeta.post_id
    AND wpostmeta.meta_key = 'surname'
    AND wposts.post_type = 'post'
    ORDER BY wpostmeta.meta_value ASC
    ";

    Your help would be much appreciated...

  2. drewbarden
    Member
    Posted 5 years ago #

    Ugh, I am trying to resolve the same issue, my two forum posts found here and here. No replies...

    Someone? Anyone?

  3. Hiranthi
    Member
    Posted 5 years ago #

    You need the page LesleyT is talking about and use the query they have as an example under 'Query based on Custom Field and Category'.

    But, in your case you're not trying to filter it with a specific category. From what I read in your own topics you want to filter 'em on custom fields only. In that case you'll have to look at the examples from that page onder 'Query based on Custom Field and Sorted by Value'.

  4. drewbarden
    Member
    Posted 5 years ago #

    Ok Fantastic. This is good. This is where I need clarification:

    I don't understand how I would create a link, leading to a posts page filtering with this code:

    $querystr = "
        SELECT wposts.*
        FROM $wpdb->posts wposts, $wpdb->postmeta wpostmeta
        WHERE wposts.ID = wpostmeta.post_id
        AND wpostmeta.meta_key = 'custom-key'
        AND wposts.post_type = 'page'
        ORDER BY wpostmeta.meta_value DESC
        ";
  5. boldhand
    Member
    Posted 5 years ago #

    to have a link to filter the posts on a page you need to create a template page with the filtering code, then link to that page. this isn't the only way I'm sure, but that's the one I know. I hope it works for you.

  6. Mark / t31os
    Moderator
    Posted 5 years ago #

    You shouldn't need to use a custom query for this at all...

    You can add "additional" parameters into query_posts.

    $query_string should contain the current query parameters, so you'd preserve that by literally adding on like so..

    query_posts($query_string . '&args');

    Where args is any number of supported arguments(parameters) as shown here.
    http://codex.wordpress.org/Template_Tags/query_posts

    So with a meta value..

    query_posts($query_string . '&meta_key=YOURFIELDNAME');
    or (if you need the custom field to match a value to)
    query_posts($query_string . '&meta_key=YOURFIELDNAME&meta_value=YOURVALUEHERE');

  7. boldhand
    Member
    Posted 5 years ago #

    I like the idea t31os_ suggested, using a custom query to filter and sort posts, I have a question, I need to have a link that when clicked, filters the posts in the page according to the custom query. Is there a way to do so?

  8. boldhand
    Member
    Posted 5 years ago #

    I have made some searching in PHP scripts and reached to a perfect solution the suits my needs, I'll type it in here in case someone needs something like this:

    this goes at the top of the page:
    <?php
    $thekey = $_GET['thekey'];
    $thevalue = $_GET['thevalue'];
    query_posts('meta_key='.$thekey.'&meta_value='.$thevalue);
    ?>

    this link goes anywhere on the same page:
    mysite.com/?thekey=TestKey&thevalue=value>test link</

    please note I'm not a PHP developer at all, so I'll sound baby talk for experts:)

    - $thekey & $thevalue: these are variables they could be anything, they are just names
    - ?thekey & ?thevalue: these are the same variables called in the URL
    - TestKey: this is the meta key I gave to the posts
    - value: this is the value I gave to the meta key (TestKey)

    adding the query_posts with the "meta key" and "meta value" arguments and made them equal to the variables

    The rest is for the GET command to do.

  9. Mark / t31os
    Moderator
    Posted 5 years ago #

    I'm pretty sure if you add the orderby parameter to what i posted before it'll work without any need to use $_GET requests..

    http://codex.wordpress.org/Template_Tags/query_posts#Orderby_Parameters

    Also this will prodcue PHP errors/warnings when no value is present.

    $thekey = $_GET['thekey'];

    At a minimum you need to check if it's set...

    if(isset($_GET['thekey'])) {
    $somevar = $_GET['thekey'];
    } else {
    $somevar = '';
    }

Topic Closed

This topic has been closed to new replies.

About this Topic