WordPress.org

Ready to get started?Download WordPress

Forums

Simple Fields
[resolved] Query Post by Custom Date Field (10 posts)

  1. Bjorn van der Neut
    Member
    Posted 1 year ago #

    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

    http://wordpress.org/extend/plugins/simple-fields/

  2. Bjorn van der Neut
    Member
    Posted 1 year ago #

    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'
    
    				)
    			);
  3. Pär Thernström
    Member
    Plugin Author

    Posted 1 year ago #

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

  4. Bjorn van der Neut
    Member
    Posted 1 year ago #

    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

  5. Bjorn van der Neut
    Member
    Posted 1 year ago #

    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

  6. Bjorn van der Neut
    Member
    Posted 1 year ago #

    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

  7. Pär Thernström
    Member
    Plugin Author

    Posted 1 year ago #

    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:

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

  8. Bjorn van der Neut
    Member
    Posted 1 year ago #

    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

  9. Pär Thernström
    Member
    Plugin Author

    Posted 1 year ago #

    I made a new field instead, that stores the date in iso-format instead:
    http://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 http://www.php.net/manual/en/function.usort.php to sort your array by timestamp.

  10. Bjorn van der Neut
    Member
    Posted 1 year ago #

    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.

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic