Creating Archive.php in wordpress using custom field

Asked

Viewed 252 times

3

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>

Then the posts are loaded via Ajax. I created a form like POST called agenda and a custom field called agenda_data.

I need help on two items, the rest I think I can handle on my own.

  1. In the select appear the last 12 months, the Wordpress function wp_get_archives does this. But it returns based on the publication dates and I’d like it to be based on the date of the custom field.

  2. A archive.php that displays posts also based on the date of the custom field and not by the date of post publication.

I haven’t found anything yet on the Internet that could help me.

  • I didn’t understand the part baseado na data do campo personalizado. What field is this? How do you customize it?

  • Shows the full form.

  • A custom field, where you inform the date of the event, understood ?

  • Sergio, could you put the code you are using? Is the value of the custom field being recorded as a date or in another format? Could put the code that creates the custom field tb?

  • Opa Ricardo, all right ? now I’m using the same native date of Wordpress. I didn’t know but it is possible to display a post even though it is scheduled for a future date, using 'post_status' => 'Future'. Now I’m just having problems loading by ajax anyway.

  • @Sérgiomachado, you managed to solve the problem by using the wp_get_archives()? And how are you doing the AJAX loading?

Show 1 more comment

1 answer

1

It seems that there is not much way, the filters available in the function wp_get_archives are not sufficient to perform this filtering. The solution is to copy the function to a meu_get_archives($args) and adapt to your needs: https://core.trac.wordpress.org/browser/tags/4.3.1/src/wp-includes/general-template.php#L1354

On the page archive.php, instead of the traditional <?php if( have_posts() ) : ?>, have to make a Wp_query customized, using the parameters Order & Orderby:

'order' => 'ASC',
'orderby' => 'meta_val_num'

Or else you could use the filter pre_get_posts, which is recommended to filter the main query and leave the template archive.php the way 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)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.