Matt Jones Tech
  • Blog
  • Video Projects
  • Web Projects
  • Using WP_Query() to sort datetime meta_value

    Just like the title suggests, I’ve spent the last several hours struggling with meta_query to return CPTs based on some datetime picker meta_value. Let me explain my setup.

    I’m writing a plugin that stores data from a custom_post_type for gym classes with a handful of custom fields put into place using ACF. One of those custom fields is gym_class_date which contains the output from a jQuery datetime picker. Here’s a look at my broken code:

     $today = date(‘Ymd H:i:s’);
    
     
    
    
     $MJCalendar = new WP_Query(array(
    
     ‘post_type’ => ‘gym_class’,
    
     ‘posts_per_page’ => –1,
    
     ‘meta_key’ => ‘gym_class_date’,
    
     ‘orderby’ => ‘meta_value’,
    
     ‘order’ => ‘ASC’,
    
     //only return upcoming classes, not classes in the past
    
     ‘meta_query’ => array(
    
     array(
    
     ‘key’ => ‘gym_class_date’,
    
     ‘compare’ => ‘>=’,
    
     ‘value’ => $today,
    
     )
    
     )
    
     ));
    
     
    
    
     while($MJCalendar->have_posts()) {
    
     $MJCalendar->the_post();

    When I ran the code, the resulting output was… nothing. So what gives?

    I burned an entire Sunday scratching my head over this and I finally figured it out. Here was my problem:

    ACF always stores datetime values in an EXTREMELY specific way: 'YYYY-MM-DD HH:ii:ss'. And I mean specific. Even though earlier in my code, I defined $today = date('Ymd H:i:s');… it was just wrong enough to break literally everything. Turns out, 20210116 16:37:24 does NOT equal 2021-01-16 16:37:24.

    Adding some simple dashes to my $today = date('Y-m-d H:i:s'); declaration made everything magically start working again.

    That’s gonna do it for this post, I know it’s a bit of a short one, but it took me FOREVER to figure out this stupid detail. Note to self, and hopefully to you, when you’re comparing two  datetime values with a meta_query, for Pete’s sake, make absolutely sure that your format is perfectly the exact same as the format coming from the database. Otherwise, none of your comparison operators will work.

    matt

    January 8, 2021
    Web Design, Web Development
    code, php, wordpress, wp_query()
  • Batch Process FFMPEG

    Batch Process FFMPEG

    An incredible use case for simple Bash scripting is the ability to batch process ffmpeg tasks. First off, I’ll assume you guys have got a basic understanding of FFMPEG, what it is, how to install and use it, that kinda thing. If not, feel free to check out the basics. FFMPEG is absolutely amazing. It can convert a chicken sandwich to ProRes. So once you’ve got the basic single-file convert down, how do you batch convert?

    Shell Scripts

    This is new for me, and I’m happy to learn new stuff. So upon a basic introduction, shell scripting looks incredibly powerful. I’m easing my way into Python because Blender is written in Python, but apparently Python is something you can integrate into shell scripts as well, so all the better!

    More good news, someone had the same issue as me, and got a pretty rad response on StackOverflow. The 2nd answer below the ‘correct’ one uses something called parameter expansion that I don’t really know a lot about. But from my limited understanding, I gather that it’s a way to take the existing filename and stick it on to the ffmpeg file export. So let’s say you have vacayFootage.avi that you want to convert to .mp4. Parameter expansion grabs the file name and sticks it onto the resulting export so you’ll get vacayFootage.mp4 without the hassle of having to name a thousand files by hand, each time you convert a file. So here’s an example of how you would batch process ffmpeg commands:

    for i in *.avi; do ffmpeg -i "$i" "${i%.*}.mp4"; done

    So like a true professional, I pasted that Stackoverflow answer into a shell script, modified the lines a bit to match my needs, and boom! It worked! I was able to convert about 500 files in just a few minutes with a single line of code… So awesome!

    So Many Possibilities…

    So with that fancy new script… and learning about how read is a cool Python command that can be used to collect user input… I’d love to write a shell script that can work for anyone. Just enter the directory where all your media is, choose your output options, select your output directory, and boom! Batch encodes for all! Gotta get on that… right after I learn Python.

    matt

    September 16, 2019
    FFMPEG, Linux, Video Editing
    bash, code, ffmpeg, file conversion, linux, python, scripting, shell

Prove all things; hold fast that which is good. 1 Thess 5:21