• Resolved kryptech

    (@kryptech)


    Hey guys,

    I have been strugguling days and days with custom querys, I have client who has a travel agency website and they need to display their so called travel offers on the site. Now they have a custom meta field called reisialgus (travelstart), that meta field contains date in format dd/mm/yy.

    I need to sort it by date. Unfortunately it does not work as it should 🙁

    Can you tell me if there is something I’m doing wrong…

    <?php $args = array(
    ‘posts_per_page’ => 50,
    ‘orderby’ => ‘reisialgus’,
    ‘order’ => ‘ASC’,
    ‘meta_key’ => ‘reisialgus’,
    ‘post_type’ => ‘post’,
    ‘post_status’ => ‘publish’,
    ‘suppress_filters’ => true );

    $the_query = new WP_Query($args);
    if( $the_query->have_posts() ): ?>
    <?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>

    Cheers,

    Ivar

Viewing 1 replies (of 1 total)
  • From looking at the docs here and looking at the structure of the _postmeta table it looks as though the key and value fields for the meta information are both text fields.

    In the documentation it says either meta_value (for strings) or meta_value_num (for numerical data) should be present. Neither of which are present in the array you are passing to WP_Query.

    I would suggest you try adding these (try one and then the other one) to see if this helps.

    I suspect because you are storing dates you may still not get quite the result you are looking for.

    If you do not then you could try some magic behind the scenes and convert dates into a number to represent the date, e.g. the number of days from a specific date. Then you could use meta_value_num for your sorting.

    Of course if you use this approach do not forget to add a function to convert the number back to a date 😉

Viewing 1 replies (of 1 total)

The topic ‘WordPress custom meta query’ is closed to new replies.