Support

Account

Home Forums General Issues need to select first item in meta_value array to sortby

Solved

need to select first item in meta_value array to sortby

  • Is it possible to select the first item in an array like below to the sortby? I am needing to sort by application type, but needing to sort it by just the first item in the array. Below is what I am using then I was tinkering around with the sortby option? Hope I’m making sense. Really would appreciate the help on how I should go about doing this! I’ve spent hours trying to figure this out!

    <?php
    $args = array(
    ‘relation’ => ‘AND’,
    ‘cat’ => 5,
    ‘post_type’ => ‘post’,
    ‘post_status’ => ‘publish’,
    ‘posts_per_page’ => -1,
    ‘meta_query’ => array(
    array(
    ‘key’ => ‘floor_type’,
    ‘value’ => $floor_type
    ),
    array(
    ‘key’ => ‘stack_type’,
    ‘value’ => $stack_type
    ),
    array(
    ‘key’ => ‘plumbing_opening’,
    ‘value’ => $plumbing_opening
    )),
    ‘meta_key’ => ‘application_type’,
    ‘orderby’ => reset($meta_value),
    ‘order’ => ‘ASC’
    );
    $posts = query_posts($args);
    ?>

  • Hi @crushdg

    Please wrap all code in the ‘code’ button to allow for easy reading.

    As for the code you have posted. I don’t understand what $meta_value is. Is this a variable which you have defined above?

    Perhaps you could explain the data you have and what you are trying to do in a clear way without any code, and I can help direct you to a solution.

    Thanks
    E

  • <?php
        $args = array(
          'relation' => 'AND',
          'cat' => 5,
          'post_type' => 'post',
          'post_status' => 'publish',
          'posts_per_page' => -1,
          'meta_query' => array(
            array(
              'key' => 'floor_type',
              'value' => $floor_type
            ),
            array(
              'key' => 'stack_type',
              'value' => $stack_type
            ),
            array(
              'key' => 'plumbing_opening',
              'value' => $plumbing_opening
            )),
          'meta_key' => 'application_type',
          'orderby' => 'meta_value',
          'order' => 'ASC'
        );
        $posts = query_posts($args);
      ?>

    Sorry, I was using the $meta_value as an example. Currently the orderby ‘meta_value’ is an array from client having saved the post with multiple values when selecting the checkboxes. Is it possibly to select just the first item in array to orderby? This make anymore sense. Thank you so much for your help!

  • Hi @crushdg

    If you review the ‘orderby’ documentation on:
    http://codex.wordpress.org/Class_Reference/WP_Query

    You will see that the ‘orderby’ => ‘meta_value’ param will then tell WP to order the posts by the field name in the param ‘ ‘meta_key’ => ‘application_type’

    So your code above looks like it will order the posts by ‘application_type’ value.
    I don’t understand what you mean by:
    Currently the orderby ‘meta_value’ is an array

    In the above code, orderby is not an array. It is a string with a value of ‘meta_value’

    Thanks
    E

  • I really appreciate all your help! Sorry I’m not doing the best job explaining or understanding.

    When I echo the results of ‘application_type’ it is printing out “Array”, unless I do $arr[0] then it will print out like “Bathtub”.

    $arr = get_field('application_type', $post->ID); echo $arr;

    I want my orderby to use just the first item in the Array, currently it won’t even work period, even if just one value in ‘application_type’. I am allowing the admin to select checkboxes, but tried allowing them to select just one value from a drop down and the orderby works great.

    Hopefully I am making a little more sense? Thank you so much for your help!

  • Hi @crushdg

    It is important that you always debug your code. You can do so like this:

    
    <?php 
    
    $arr = get_field('application_type', $post->ID);
    echo '<pre>';
    	print_r( $arr );
    echo '</pre>';
    die; 
    
    ?>
    

    This will show you the data you have. When you see ‘array’, it means that the variable is an array, not a string. Therefore, it can’t be echo’ed. Please research arrays on google to learn more.

    A checkbox will always return an array because you can select multiple values.

    To get the first checked value fromt he array you can do so like this:

    
    $arr = $arr[0];
    

    Does this help?

    Thanks
    E

  • I’m doing a terrible job of explaining. I’m having no problems with viewing the data in the arrays and realize it’s an array and been pouring over google.

    But how do I in my original code tell the ‘orderby’ to literally order the meta_value by like ‘$arr[0]’? Or is this not even possible? Currently the ‘orderby’ will not work because it’s an array, so want it to look at just the first value in the array to ‘orderby’.

    Really appreciate your help, hopefully I made a little more sense this time.

  • Hi @crushdg

    I don’t understand what you mean by:
    Currently the ‘orderby’ will not work because it’s an array, so want it to look at just the first value in the array to ‘orderby’.

    Your code shows:
    'orderby' => 'meta_value', – this is NOT an array. This is a string telling WP to order your posts by the meta_value of the field defined in the param:
    'meta_key' => 'application_type',

    Are you trying to say that the value for ‘application_type’ is an array?

    If so, then no, there is no way for the WP_Query object to correctly order the posts on the custom field value, instead. You will need to order the posts with some custom logic AFTER you load the posts.

    Hope that helps.

    Thanks
    E

  • Yup, that’s what I was wondering. That stinks. Was hoping I wouldn’t have to do it after fetching. Haven’t seen any articles are sorting after fetching the posts have you?

    Thanks for helping me out on here!

  • Hi @crushdg

    You can easily sort the posts after the query because they are returned as an array.

    The query_posts function will override the post objects, for the global $wp_query object. You can access the posts like so:

    
    global $wp_query;
    
    $current_posts = $wp_query->posts;
    

    You can then use some PHP to loop over the posts and re-order them. There are lots of different ways to re-order an array which I will leave with you to research and discover as this is more of a general WP question now.

    Hope that helps.

    Thanks
    E

  • Was able to figure out a solution. Really appreciate the help!

Viewing 11 posts - 1 through 11 (of 11 total)

The topic ‘need to select first item in meta_value array to sortby’ is closed to new replies.