问题
here's my situation: my client's wordpress site has an "events" custom post type with a custom field called "date" (advanced custom field's datepicker). The site works just fine in all its parts but now they asked me to add a widget-like calendar that fetches all the events.
Basically what i need is something like get_calendar(); but for the "events" post type that uses the "date" custom field instead of the creation date. I spent the whole day yesterday looking for a plugin, hack, snippet of code... but nothing seems to do the job. I know there are similar questions even here on stackoverflow, but still, no solution...
Do you have any ideas?
回答1:
OK, so what you need is a widget that has custom query args that will fetch the data you need. You can get the info on widget creation here, and ACF documentation is here. The custom query args should include your custom post type and the ACF data you need. For example:
<?php
$args = array (
'post_type' => 'events', // your custom post type
'meta_query' => array(
'relation' => 'AND', // includes ACF stuff
array(
'key' => 'date', // the specific key of your ACF
'compare' => '='
)
)
);
$query = new WP_Query($args);
?>
This will fetch all the posts from the database from your custom post type that have a date field set. You can modify the value to fit the exact date as well. Or do it through the widget by forwarding a variable for a specific date entered
来源:https://stackoverflow.com/questions/26651304/wordpress-calendar-with-custom-post-type-custom-fields