I have created a very simple website for a client where he

Hey,

I have created a very simple website for a client where he can add and show upcoming auctions.

I have used the Custom Post Types UI plugin for the post types and the Advanced Custom Field to display the meta-fields.

Now, on the frontpage http://auktionsvinder.dk you can see three events. Now, the test event I have created, is "old". Or in other words, it has passed the date for the event, so; it should NOT be showing any longer.

How can make a custom WP Query that filters old post and only showing today's event and future event?

This is my current WP_Query, which doesn't seem to be working:

$args = array(
	'post_type' => 'auktioner',
	'meta_key' => 'dato',
	'orderby' => 'meta_value_num',
	'order' => 'ASC',
    'meta_query' => array(
     array(
     	'key' => 'dato',
     	'compare' => '>=',
     	'value' => date('Y-m-d')
     	))
	);

$the_query = new WP_Query( $args );

Thanks a lot!
Sincerely,
Mika

  • Vaughan
    • Support/SLS MockingJay

    Hi @mika,

    Can you try changing the date line.

    $args = array(
    	'post_type' => 'auktioner',
    	'meta_key' => 'dato',
    	'orderby' => 'meta_value_num',
    	'order' => 'ASC',
        'meta_query' => array(
           	'key' => 'dato',
         	'compare' => '>=',
         	'value' => current_timestamp('timestamp'),
            'type' => 'numeric'
         	),
            	'meta_value' => current_time('timestamp'),
    		'meta_value_num' => current_time('timestamp'),
    		'meta_compare' => '>='
    	);

    Hopefully that should work.

    Cheers

  • Mika
    • The Bug Hunter

    Hey Vaughan

    Thanks for your reply.

    I get an error message saying: "Fatal error: Call to undefined function current_timestamp() in /home/auktionsvinder/public_html/wp-content/themes/twentytwelve-child/auktioner.php on line 33"

    Sincerely,
    Mika

  • Mika
    • The Bug Hunter

    Hey Vaughan.

    It seems like my client deleted it from the list since it is a live running website. Now, I have given you full access to the website through the WPMU DEV support plugin system, so you are more than welcome to go to the website and create an test auction under custom post type "Auktioner" to check it out :slight_smile:

    Sincerely,
    Mika

  • Jose
    • Bruno Diaz

    Hey @Mika,

    It is working on your site now.

    This is the correct query:

    $args = array(
    
    	'post_type' => 'auktioner',
    
    	'meta_key' => 'dato',
    
    	'orderby' => 'meta_value_num',
    
    	'posts_per_page' => 999,
    
    	'order' => 'ASC',
    
        'meta_query' => array(
    	    	array(
    
    	       	'key' => 'dato',
    
    	     	'compare' => '>=',
    
    	     	'value' => date(Ymd),
    
    	        'type' => 'DATE'
    
    	     	),
    
         	),
    
    	);
    
    $the_query = new WP_Query( $args );

    The meta value 'dato' is stored inthe format YYYYMMDD, so you need to compare against the current date in the same format: date(Ymd).

    Also, the meta_query argument expects a multidimensional array as parameter (aka an array of arrays). This is to allow multiple conditions and relations.

    Please let me know if it works correctly now and don't hesitate to ask if you have further questions. I feel that I'm almost understanding Danish :wink:

    Take care,
    Jose

  • Mika
    • The Bug Hunter

    Hey Jose.

    Amazing! Thank you so much!!!!!!!!!! I really can't thank you enough.

    Could you explain each section in the last array to me, so I can get a better understanding of how it works in practice for future references?

    array(
    
    	       	'key' => 'dato',
    
    	     	'compare' => '>=',
    
    	     	'value' => date(Ymd),
    
    	        'type' => 'DATE'
    
    	     	),

    Additionally, I love your php work! Where would you recommend me to find good teaching material on PHP so I can reach your level one day? :smiley:

    LOL, danish is easy speaking of grammar, however the pronunciation is quite another story :wink:

    Sincerely,
    Mika

  • Jose
    • Bruno Diaz

    @Mika,

    I'm really happy to hear that everything works now!.

    The array is quite simple:

    'key' => 'dato' // The first value to compare. Refers to the post metadata with meta_key equal to 'dato'. In this case, it retrieves a date in the format YYYYMMDD. i.e. 20140916. You can find this metadata stored in the DB under the table prefix_postmeta.

    'compare' => '>=' // this is the comparison operator that you will use to compare 'key' and 'value'. In this case '>=' (greater or equal to)

    'value' => date(Ymd) // The second value to compare. It should be the same type of value as the first value. In this case, we need to use the 'today' date in the format YYYYMMDD.

    'type' => 'DATE' // The type of data being handled. This is not always required. It may work using NUMERIC also. But just in case :slight_smile:

    In the background, WP will take this arguments and build a WHERE clause for the SQL query to get only the posts where the metadata 'dato' is greater or equal to date(Ymd).

    Hope the explanation is clear. Just ask me if you have specific questions.

    Additionally, I love your php work! Where would you recommend me to find good teaching material on PHP so I can reach your level one day? :smiley:

    Thanks!. I do not recommend any specific material or tutorial. The best way to learn, in my opinion, is the hard way: just try to code things and always use some debug method to figure out what is going on in your code. You can use the very basic var_dump or the most evolved debugging extensions for your IDE.

    Of course, there are some good places to get information when you are stuck. My favorites are usually the official documentation of each technology:
    - PHP: http://php.net/manual/en/
    - Wordpress: http://codex.wordpress.org/ and the new reference for WP 4.0 http://developer.wordpress.org/reference/.
    - jQuery: http://learn.jquery.com/ and http://api.jquery.com/

    And you can always learn good stuff (and also really bad stuff, so watch out) from Stackoverflow:
    - http://stackoverflow.com/

    I'm sure you'll become a PHP ninja soon :wink:

    kærlig hilsen!

  • Mika
    • The Bug Hunter

    Hej med dig :smiley:

    Lol, you will soon become a Danish teacher :wink:

    Absolutely! It makes completely sense. Thanks a lot for the clarification, it was very helpful and has given me some extra tools in my toolbox for upcoming projects!

    Once again Jose, I really appreciate your help and the resources reference! :slight_smile:

    Kærlig hilsen
    Mika

  • Mika
    • The Bug Hunter

    Hey Jose.

    I have noticed one last thing with the project, which I do not know if there is a way around.

    The query is order by ASC, which is good. However, each auction says either (here we go again with more danish lessions :wink:)

    Starter (starts)

    or

    Slutter (ends)

    This gives an issues since it through off the orderby function. What I mean is this:

    18/09/2014 starter kl. 10:00 Odense Auktionerne - Indbo auktion Åbner i nyt eksternt vindue (5000 Odense C)

    18/09/2014 slutter kl. 18:00 York Auktion - LIVE AUKTION Åbner i nyt eksternt vindue (4296 Nyrup)

    18/09/2014 slutter kl. 12:00 damonline - 1065 - Lingeri (Calvin Klein m.v.), Skjorter (Selected m.v.) Åbner i nyt eksternt vindue (7130 Juelsminde)

    As you can see, all events are on the same day. However, the last event is at 12:00 which is later than 18:00 - The reason it is because it is the time it SLUTTER and the top one is the time it STARTER.

    Make sense? :smiley:

    Sorry to add to this topic now that we have closed it.

    Sincerely,
    Mika

  • Mika
    • The Bug Hunter

    Hey Jose.

    It is only possible to add one time. Then there is a filed which says "start" or "end" but it is just a radio button section and should not really interfere with the for YYYYMMDD order.

    However, I think the issues is with the order ASC since the letters before the event is START or SLUTTER.

    Sincerely,
    Mika :slight_smile:

  • Mika
    • The Bug Hunter

    The expected order, no matter start and end, would be the chronological order of time.

    So, if an event starts at 13:00 and another ends at 13.30, the one that starts at 13 would come first :slight_smile:

    Sincerely,
    Mika

  • Jose
    • Bruno Diaz

    Hey Mika,

    Unfortunately, WP_Query do not support multiple meta keys sort.

    The correct way to go here would be to change your custom fields to unify the date and the time into a single field.

    This is something relatively easy by just changing the 'dato' field from Date Picker to Date and Time Picker.

    Then, I believe that ACF will store the value as a timestamp, therefore you will need to change the query arguments to this:
    array(

    'key' => 'dato',

    'compare' => '>=',

    'value' => time(),

    'type' => 'NUMERIC'

    ),

    Let me know if you have any issues with this modification.

    Cheers!
    Jose

  • Mika
    • The Bug Hunter

    Hey Jose.

    Yes that makes completely sense. The only issue that it forces me to also use the time picker when shifting to the date and time picker, so I'm not quite sure how to add the get_field info with that option?

    Do I now merge them together or do I keep them separate? Additionally, if I merge them together, will I loose the current times?

    Can you help me out here? :slight_smile:

    Sincerely,
    Mika

  • Jose
    • Bruno Diaz

    Mika,

    Do I now merge them together or do I keep them separate?

    I can't see what are the pros on keeping them separate. From the UX perspective, it makes more sense to merge both fields. From the code perspective, it is definitively better to have them merged.

    Additionally, if I merge them together, will I loose the current times?

    Yes, you'll need to set them back by hand and re save each post.

    The only issue that it forces me to also use the time picker when shifting to the date and time picker, so I'm not quite sure how to add the get_field info with that option?

    Didn't get your point here. Can you please clarify?

    Cheers!

    • Mika
      • The Bug Hunter

      Hey Jose.

      Thanks for your reply.

      1. So I need to save the current dates somewhere and then redo them again?
      2. Regarding the last point I didn't made quite clear: How would I now output the the_field('tidspunk') and the_field('date')? Since I add two kind of information, will it now become and array with the info? If yes, how do I get the info from the array? :slight_smile:

      Sincerely,
      Mika

      • Jose
        • Bruno Diaz

        Mika,

        1. So I need to save the current dates somewhere and then redo them again?

        Yes, something like that. You may want to create a new field for the merged date time, so you can copy the values from the old fields. Then, you need to change the code to match the name of the new field.

        2. Regarding the last point I didn't made quite clear: How would I now output the the_field('tidspunk') and the_field('date')? Since I add two kind of information, will it now become and array with the info? If yes, how do I get the info from the array? :slight_smile:

        Good question. No, it is not saved as array. It is saved as unix timestamp.
        You will need to set the field option "Get field as timestamp?" to "yes". That way you can use the php date method to output the tidspunk and the dato:

        //These two lines would be equivalent
        the_field('tidspunk')
        echo date('h:i:s', get_field('merged_date'));

        Let me know if you are stuck at some point.

        Cheers!
        Jose

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.