1
query_posts($query_string . '&meta_value=pro&orderby=rand');

I get pro list with random, but I want pro as well as free, but with pro list first, then meta_value free list.

I also set orderby meta_value

    query_posts($query_string . '&orderby=meta_value_num');

This does not work, and in query string default for cat. It will take orderby date with both meta value = pro and free.

Any suggestions?

6
  • Welcome to WPSE. To start you off, please feel free to visit our tour page to get a feel on how the site operate. Also visit help center for any site related questions. To come back to your question, I just need to clear some issues, you want to randomly fetch posts from a meta key with free and pro as meta values. Say you get 10 posts, 4 pro and 6 free, the 4 pro posts must be shown first, then the other 6 free. Just a tip, do not use query_posts, rather use WP_Query – Pieter Goosen Oct 13 '14 at 12:13
  • k thanks means i used query_post in - inbuild function of theme. tax-list.php so i need to create new cat.php file ? for new loop ? or used wp in same file ? – itcgpit mark Oct 13 '14 at 12:44
  • Use the same file, just change query_posts to WP_Query. Also, please advice on my last comment whether this is what you are trying to do – Pieter Goosen Oct 13 '14 at 12:47
  • ya i am trying like. in page of category listing post type = listing meta value = pro and free but first display pro then free .so order by meta value order DESC WP_Query($query_string . '&orderby=meta_value_num'); Call to undefined function WP_Query() error – itcgpit mark Oct 13 '14 at 13:00
  • for ex in my category page if there 10 post then i want 10 with random but meta value = pro first then meta_value = free then. ok . right now random working .with all or only post or only free. but i want order by meta value so easy for me to solve this issues – itcgpit mark Oct 13 '14 at 13:05
2

Before I fire away, just one note, NEVER (my emphasis) make use of query_posts to create custom queries

Note: This function isn't meant to be used by plugins or themes. As explained later, there are better, more performant options to alter the main query. query_posts() is overly simplistic and problematic way to modify main query of a page by replacing it with new instance of the query. It is inefficient (re-runs SQL queries) and will outright fail in some circumstances (especially often when dealing with posts pagination).

Rather make use of WP_Query or get_posts to create custom queries, but ONLY if you can't modify the main query with pre_get_posts. For more info, check out this post

Assuming that there are only two values for your meta_key (PLEASE NOTE: For testing purposes, I have used my own meta key, you should replace this with your own), namely pro and free, you can just retrieve posts that has this specific meta_key. Your query will then look something like this:

$args = [
    'meta_key'          => 'packages',
    'orderby'           => 'rand',
    'posts_per_page'    => 5,
];

$the_query = new WP_Query( $args );

If you however have more values than just these two, then you will need to adjust your query to include the meta_value and then make use of the meta_compare parameter. Your adjusted query will then become:

$args = [
    'meta_key'          => 'packages',
    'meta_value'        => 'free, pro',
    'orderby'           => 'rand',
    'meta_compare'      => 'IN',
    'posts_per_page'    => 5,
];

(PS! Before I go on, you should note that the orderby parameter has changed in version 4.0, but this will not work in this instance.

Great, you will now have 5 posts that was retrieved randomly which has either a meta_value of pro or free

Now we need to sort them. The best possible solution is to make use of usort to sort the returned array of posts in $the_query->posts according to the value of the posts meta_value. You will then need to unset $the_query->posts and then reset it with the reordered post array.

Here is the complete code:

<?php
$args = [
    'meta_key'          => 'packages',
    'meta_value'        => 'free, pro',
    'orderby'           => 'rand',
    'meta_compare'      => 'IN',
    'posts_per_page'    => 5,
];

$the_query = new WP_Query( $args );
$post_metas = $the_query->posts;

usort( $post_metas, function( $a, $b ){
    $a = get_post_meta( $a->ID , 'packages', true ) ;
    $b = get_post_meta( $b->ID, 'packages', true ) ;

    if ( $a == $b ) {
        return 0;
    }
        return ( $a > $b ) ? -1 : 1;
    }
);

unset($the_query->posts);
$the_query->posts = $post_metas;

if ( $the_query->have_posts() ) {
    echo '<ul>';
    while ( $the_query->have_posts() ) {
        $the_query->the_post();

        echo '<li>' . get_post_meta( get_the_ID(), 'packages', true ). '</br>' . get_the_title() . '</li>';

    }
    echo '</ul>';
} 
wp_reset_postdata();
?>

You will just need to adjust the query variables to suit your needs and also adjust the loop to display what is needed

This is not the only way to achieve it though, you can also move your sorting function to your functions.php and specifically target this custom query

6
  • ye i got pro first as well as random thanks . but now its display result as all post.. but i want only for perticuler category. like i click on categoryand i want post of only that category thanks also my post type = listing – itcgpit mark Oct 15 '14 at 4:44
  • Just add 'cat' => get_queried_object('cat')->cat_ID, to your arguments – Pieter Goosen Oct 15 '14 at 5:10
  • thanks i got perfect output in database. when i pass output sql. now i just need to change in html means echo part. asl but right now its pro after free but i want that value as random .?.thanks i also need to changes in html or what i want to display in page thanks – itcgpit mark Oct 15 '14 at 5:55
  • Glad that is solved. As for the HTML section, that is out of scope of this question. HTML related stuff is also off topic on this site. If you need help with the html structure of your page, you should ask a new question over at stackoverflow.com which is better suited for these type of questions. Thank you – Pieter Goosen Oct 15 '14 at 6:10
  • thanks a lot u saved my day. i got right data now just need in echo. an image title and des. with as per theme formate. ok thanks sir :) – itcgpit mark Oct 15 '14 at 6:31
0

You need to query by the meta_key instead of meta_value:

 $query_string . '&meta_key=name_of_key_that_stores_free_or_pro&orderby=meta_value_num'
3
  • thanks for reply but its not working i used query_posts($query_string . '&meta_key=J_listing_type&orderby=meta_value_num'); but not working – itcgpit mark Oct 13 '14 at 9:56
  • Try just orderby=meta_value – TheDeadMedic Oct 13 '14 at 9:59
  • i try this also but not working :( – itcgpit mark Oct 13 '14 at 11:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.