Support » Fixing WordPress » Custom select query – sort by meta value within category

  • Hi, I’ve tried all sorts of ways to make this work, and have investigated options in 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…

Viewing 8 replies - 1 through 8 (of 8 total)
  • Ugh, I am trying to resolve the same issue, my two forum posts found here and here. No replies…

    Someone? Anyone?

    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‘.

    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

    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.

    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.

    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');

    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?

    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:
    $thekey = $_GET[‘thekey’];
    $thevalue = $_GET[‘thevalue’];

    this link goes anywhere on the same page:>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.

    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..

    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 = '';

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘Custom select query – sort by meta value within category’ is closed to new replies.