MEC Looper Order

Hi,

I’ve been using this thread to build a query to pull in MEC featured images:

The only problem I’m running into is trying to get it to Order By using the mec_start_date. When I use the &orderby=meta_key, it sorts alphabetically. BUT, when I use meta_value_num it is sorting correctly, BUT for some reason most of my items disappear (I have conditionals set for a certain category as I could not get the taxonomy part of the query string to work).

Here is what I’m using:

post_type=mec-events&post_status=publish&meta_key=mec_start_date&meta_type=DATE&meta_compare=>=&meta_value={{dc:global:date format=“Ymd”}}&orderby=meta_value_num

I have my looper consumer as a grid cell and it has a conditional to only show the posts with the tag “Take Action”.

Here is the page it is on (at the bottom): https://powercoalition.org/takeaction/

Any ideas? Or is this beyond the scope of what can be offered?

Thanks!
Jen

Hello Jen,

Thanks for writing to us.

Would you mind sharing your admin credential so that we can check on your setup clearly? To do that, please give us the following information in a Secure Note.

  • WordPress Login URL
  • Admin level username and password

You can find the Secure Note button at the bottom of your posts.

Thank you.

Thank you. Sorry for the delay, I was unable to access the webpage for some unknown reason. The credentials are in the Secure Note.

Hi Jen,

The Mec Start Date is DATETIME field and store the date and time both, I would suggest you use the DATETIME as meta_type and meta_value_datetime for orderby.

Hope it helps.
Thanks

It doesn’t seem to work still. It is still sorting by the Wordpress date instead of by the Mec Start Date.

post_type=mec-events&post_status=publish&meta_key=mec_start_date&meta_type=DATETIME&meta_compare=>=&meta_value={{dc:global:date format=“Ymd”}}&orderby=meta_value_datetime

Is there anything else you can see that I may be doing wrong with this? I’m new to wpquery and would really like to get this figured out so that I can pull in Mec Events in other places as well.

Thank you so much!
Jen

Hello Jen,

Your WP Query is not entirely correct. If you follow the detailed explanation of @Kory on this thread (Looper Query String: Featured Products), you should be having something like this:

$meta_query[] = array(
    'key'     => 'mec_start_date',
    'value'   => date('Ymd'),
    'compare' => '>=',
);

$tax_query[] = array(
    'taxonomy' => 'mec_category',
    'terms'    => 'take-action',
);

$query = array(
    'post_type'  => 'mec-events',
    'tax_query' => $tax_query,
    'meta_key'   => 'mec_start_date',
    'orderby'    => 'meta_value',
    'order'      => 'ASC',
    'meta_query' => $meta_query
);

By utilizing http_build_query() , which will format our string accordingly:

var_dump( http_build_query( $query ) );

We can have this:
post_type=mec-events&tax_query%5B0%5D%5Btaxonomy%5D=mec_category&tax_query%5B0%5D%5Bterms%5D=take-action&meta_key=mec_start_date&orderby=meta_value&order=ASC&meta_query%5B0%5D%5Bkey%5D=mec_start_date&meta_query%5B0%5D%5Bvalue%5D=20211028&meta_query%5B0%5D%5Bcompare%5D=%3E%3D

Using this query, you do not need a condition because the Looper will only output all the events that have a “Tale Action” tag and it will be ordered by MEC start date.

Hope this makes sense.

Hi,

Thank you for your reply. Where do I use the http_build_query()? I’m unsure of how to run this function as I’ve never done that before and I can’t seem to find anything about that when I google it. Do i put it in the functions in Wordpress?

When I paste in the query that you built, nothing is showing up and it is giving me an error in the builder. I had tried building it that way to use the taxonomy but had the same problem with no events showing up. that’s why i used conditionals to do the Take Action. And when I view the page live, it is showing broken images as it is trying to call in ones that are not tagged with the category “take_action”.

Thanks again for any help you can provide.
Jen

Hello Jen,

If you check this thread: Looper Query String: Featured Products, the code should be added in your child theme’s functions.php file in order to get the query string. The correct query should be:

$meta_query[] = array(
    'key'     => 'mec_start_date',
    'value'   => date('Ymd'),
    'compare' => '>=',
    'type' => 'DATE'
);

$tax_query[] = array(
    'taxonomy' => 'mec_category',
    'terms'    => 'take-action',
);

$query = array(
    'post_type'  => 'mec-events',
    'posts_per_page' => -1,
    'tax_query' => $tax_query,
    'meta_key'   => 'mec_start_date',
    'orderby'    => 'meta_value',
    'order'      => 'ASC',
    'meta_query' => $meta_query
);


var_dump( http_build_query( $query ) );

And then you can get the query string:
post_type=mec-events&posts_per_page=-1&tax_query%5B0%5D%5Btaxonomy%5D=mec_category&tax_query%5B0%5D%5Bterms%5D=take-action&meta_key=mec_start_date&orderby=meta_value&order=ASC&meta_query%5B0%5D%5Bkey%5D=mec_start_date&meta_query%5B0%5D%5Bvalue%5D=20211029&meta_query%5B0%5D%5Bcompare%5D=%3E%3D&meta_query%5B0%5D%5Btype%5D=DATE

Best Regards.

Hi,

Thank you again. However, this still isn’t working. Should the & be just “&”? When I try it that way I’m having the same problem as before…it’s pulling in things that aren’t take-action.

Also, what is supposed to happen when I add the query to functions.php? Is there somewhere I’m supposed to go to retrieve the query string output?

I’m guessing at this point there isn’t much more I can do to get it to orderby the correct way. Thank you again for all of you assistance.

Thanks
Jen

Hello @petitshoo,

The query should return all your 19 Take Action events.

If you put the code in your child theme’s function.php file, it will give you the query string behind output to the top of the dashboard. It is because of the line var_dump( http_build_query( $query ) );. You can remove it later on once you have the query string which you can then use in the Looper Provider.

Best Regards.

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.