Support

Account

Home Forums General Issues Sort by customfield (price)

Solved

Sort by customfield (price)

  • Hey,

    I have a problem and this time, searching in the web and in the documentation didn’t help me. Maybe sombody of you can help me – that would be awesome 🙂

    So I try to filter my posts by my advanced custom field “price”. It’s a numeric field.

    $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;	
    		
    $args = array(
    	'post_type' => 'post',
    	'posts_per_page' => 8,
    	'paged' => $paged, 
    	'meta_key' => 'price',
    	'orderby'  => 'meta_value_num', 
    	'order'	=> 'ASC'
    );
    		
    $wp_query = new WP_Query($args);
    
    if(have_posts())
    {
    	while ( have_posts() ) : the_post(); 
    	echo get_field('name')
    	endwhile;
    }

    But unfortunately this doesn’t work. This Query just show me the normal order, there is no difference.

    What am I doing wrong?

    Thanks, Malte

  • try 'orderby' => 'meta_value meta_value_num',
    but everything is right
    thats weird

  • Hey VoiD2008,

    thanks for your answer! I tried this, but it makes no difference.

    This is the link to my site. There I display the array $args, maybe this will help you.

  • umm I saw just array(3) { ["post_type"]=> string(4) "post" ["posts_per_page"]=> int(8) ["paged"]=> int(1) }
    but there was no ` ‘meta_key’ => ‘price’,
    ‘orderby’ => ‘meta_value_num’,
    ‘order’ => ‘ASC’
    `

    and are you sure that your price is really numeric?
    if your sql server locale uses ‘.’ as divider your ‘45,00’ will not sort as number

  • ops my mistake, didn’t select the sorting option

  • try to check get_post_meta($post->ID,'preis');
    in your loop

    $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;	
    		
    $args = array(
    	'post_type' => 'post',
    	'posts_per_page' => 8,
    	'paged' => $paged, 
    	'meta_key' => 'price',
    	'orderby'  => 'meta_value_num', 
    	'order'	=> 'ASC'
    );
    		
    $wp_query = new WP_Query($args);
    
    if(have_posts())
    {
    	while ( have_posts() ) : the_post(); 
    var_dump(get_post_meta($post->ID,'preis'));
    	echo get_field('name')
    	endwhile;
    }
  • Sorry I forget to tell you: You have to use the dropdown menu on the bottom right “Sortieren nach:” and then select one of the options “preis aufsteigend” or “preis absteigend”. Then $args changes but not the post list.

    Yes I am sure. I looked in the database and the meta_value is for example “27.89”, so it is divided by a . Also the advanced custom field is set to numeric.

    Thanks for your time!!

  • I add your code, but the price already stands there. But maybe it will help you 🙂

    I noticed that at “Platz 5” the array has two entries, is that maybe a problem?

  • your meta preis stored in database in serialized form i guess so sorting not working

  • What does that mean? Can I do something so that it works?

  • check this

    $preis = $GLOBALS['wpdb']->get_results( 
    	"
    	SELECT meta_value 
    	FROM $wpdb->postmeta
    	WHERE meta_key = 'preis' 
    	"
    );
    var_dump($preis);
    
  • Then I get an empty array.

    //Ok now there is an output

  • got it, I was wrong values stored in right format

    check please SQL request adding this code…
    and remove all previous debug codes =)
    echo "Last SQL-Query: {$wp_query}";

  • sorry forgot keyword request=))
    echo "Last SQL-Query: {$wp_query->request}";

  • wrong place -))) put it after your query

  • after the line with $wp_query = new WP_Query($args);

  • do you have any sorting plugins installed?

    SELECT SQL_CALC_FOUND_ROWS wp_posts.ID FROM wp_posts INNER JOIN wp_postmeta ON (wp_posts.ID = wp_postmeta.post_id) WHERE 1=1 AND wp_posts.post_type = ‘post’ AND (wp_posts.post_status = ‘publish’) AND (wp_postmeta.meta_key = ‘preis’ ) GROUP BY wp_posts.ID ORDER BY wp_posts.menu_order, wp_postmeta.meta_value,wp_postmeta.meta_value+0 ASC LIMIT 0, 8

    wp_posts.menu_order should not be here…

  • check this

        global $wp_filter;
        print_r( $wp_filter['posts_orderby'] );

    somewhere near your sql dump

  • Yes I had a plugin called “Post Types Order”. Now I deactivated it and wp_posts.menu_order disappeared. But order is still not working.

    Thanks!

  • and change 'orderby' => 'meta_value meta_value_num', to 'orderby' => 'meta_value_num', back

  • There is no output or have I add it to the wrong place? :S
    I removed meta_value and now the posts are moving, but not in the right order.

  • yep i see, they sort only ascending but not descending
    let me think for a minute=))

  • btw you have to add sorting order GET var to your paginator also -))

  • No my mistake!! Sorry 😀
    I forget to remove both meta_value 😀

    But why does it work now? I just added a lot of debug code?

    Thanks you are my hero!!

    //Ah the plugin…

Viewing 25 posts - 1 through 25 (of 31 total)

You must be logged in to reply to this topic.