• Hello,

    I can’t figure out why it does not sort correctly on my custom date field.

    The Field Group slug = field_group_1
    The Field slug = wedstrijd_datum

    And this is my code.

    query_posts(
    				array(
    					'showposts' => 50,
    					'post_parent' => $this_page_id,
    					'posts_per_page' => 50,
    					'post_type' => 'page',
    					'meta-key' => 'wedstrijd_datum',
    					'orderby' => 'meta-key',
    					'order' => 'ASC'
    
    				)
    			);
    			while (have_posts()) { the_post();

    In my code I get the date with this php script:

    $selected_value = simple_fields_get_post_value(get_the_id(), "Datum", true);

    I also changed this:

    'orderby' => 'meta-key'
    into
    'orderby' => 'wedstrijd_datum'
    or
    'orderby' => 'Datum'

    But no succes yet ??

    Hope someone can help me out.

    Thanks,
    Bjorn

    https://www.ads-software.com/extend/plugins/simple-fields/

Viewing 9 replies - 1 through 9 (of 9 total)
  • Thread Starter Bjorn van der Neut

    (@neutcomp)

    Hum,

    I found out that in the database table wp_postmeta there is a field with another name: “_simple_fields_fieldGroupID_1_fieldID_1_numInSet_0”

    I changed it now to this but still no luck:

    query_posts(
    				array(
    					'showposts' => 50,
    					'post_parent' => $this_page_id,
    					'posts_per_page' => 50,
    					'post_type' => 'page',
    					'meta-key' => '_simple_fields_fieldGroupID_1_fieldID_1_numInSet_0',
    					'orderby' => 'meta_value',
    					'order' => 'ASC'
    
    				)
    			);
    Plugin Contributor eskapism

    (@eskapism)

    Try and change “meta-key” to “meta_key” and see if that makes any difference.

    Thread Starter Bjorn van der Neut

    (@neutcomp)

    Par,

    Thanks for the quick reply. It changed the order but it seems like its now completly random ??

    'orderby' => 'meta_value',
      'meta_key' => '_simple_fields_fieldGroupID_1_fieldID_1_numInSet_0',
      'order' => 'ASC'

    Any other solution?

    Thanks,
    Bjorn

    Thread Starter Bjorn van der Neut

    (@neutcomp)

    Aha I see what it does now ??

    Its sorts only on day and not yet with month and year. So it looks something like this now:

    2 june
    4 may
    6 june
    7 april

    Thread Starter Bjorn van der Neut

    (@neutcomp)

    Par,

    I found something interesting. I debug-ed the query and run it directly on the mysql database. As you can see in the screenshot is not ordered correctly like I put in my previous message.

    Screenshot result

    The strange thing here is that I don’t know why? Do you have a clue? Or even better an solution ??

    Thanks for your help.
    Bjorn

    Plugin Contributor eskapism

    (@eskapism)

    Ah, now I remember that another user had this problem. The format in the database is far from perfect when it comes to sorting. Please see this question + solution over at wordpress anwers:

    https://wordpress.stackexchange.com/questions/70473/custom-query-object-with-simple-fields-custom-date-field/70500#70500

    Thread Starter Bjorn van der Neut

    (@neutcomp)

    Par,

    What I want to do is your option 3)

    (Three: Wait a couple of days and let me update Simple Fields with support for storing the dates as unixdates, since it makes so much more sense!)

    As I can see you have a option “Enable Extended Return Values” with the description

    Return an array with the selected date as a unix timestamp and as the date format set in WordPress settings.

    But how can I sort then on the timestamp?
    Because when I do this:

    $selected_value = simple_fields_get_post_value(get_the_id(), "Datum", true);
    print_r($selected_value);

    I see this as an return value but the order is still the same:

    Array ( [saved_value] => 14/07/2013 [timestamp] => 1373760000 [date_format] => 14 July 2013 [date_format_i18n] => 14 juli 2013 )

    Hope you can help me out.
    Bjorn

    Plugin Contributor eskapism

    (@eskapism)

    I made a new field instead, that stores the date in iso-format instead:
    https://simple-fields.com/documentation/field-types/date-and-time-picker/

    You can use that new field, or use my method mentioned in the thread above to manually to a sql query to get to correct order, or you can get all your values like you did above, and then use https://www.php.net/manual/en/function.usort.php to sort your array by timestamp.

    Thread Starter Bjorn van der Neut

    (@neutcomp)

    Thanks I used the usort method. Not the nicest and fastest way but it does the job ??

    Why do you only store the date/time picker in iso format and not also the date picker? That would solve a lot of issues I think.

    Because if you now use both you need different ways to show the data.

Viewing 9 replies - 1 through 9 (of 9 total)
  • The topic ‘Query Post by Custom Date Field’ is closed to new replies.