Question:
I'm trying to create a calendar page in WordPress where the user selects the month by a select
:
<select>
<option>Janeiro 2015</option>
<option>Dezembro 2014</option>
<option>Novembro 2014</option>
<option>Outubro 2014</option>
<option>Setembro 2014</option>
</select>
Posts are then loaded via Ajax. I created a POST
form called agenda
and a custom field called agenda_data
.
I need help on two items, the rest I think I can unroll on my own.
-
In
select
the last 12 months appear, the WordPress functionwp_get_archives
does this. However it returns based on the published dates and I would like it to be based on the date of the custom field. -
An
archive.php
that displays the posts also based on the custom field date and not the post publish date.
I haven't found anything on the internet that could help me.
Answer:
It seems that there is not much way, the filters available in the wp_get_archives
function are not enough to do this filtering. The solution is to copy the function to a meu_get_archives($args)
and adapt it to your needs: https://core.trac.wordpress.org/browser/tags/4.3.1/src/wp-includes/general-template.php #L1354
As for the archive.php
page, instead of the traditional <?php if( have_posts() ) : ?>
, you have to do a custom WP_Query , using the Order & Orderby parameter:
'order' => 'ASC',
'orderby' => 'meta_val_num'
Or you could use the pre_get_posts
filter, which is recommended to filter the main query and leave the archive.php
template as it is.
References:
– Order Custom Post Type Archive by multiple values in functions.php
– Archive Listings Filtered by Date Values in a Custom Field/Post Meta? (here Mike Schinkel's answer is a master class )