dimanche 1 février 2015

Custom post types archive


I'm developing my first custom theme from scratch (using Roots as base framework). The theme has 2 CPT, one for events and one for associations with a correlation one to many between the two (an association can have many events associated).


I modified the main query with pre_get_posts to display events in homepage.


Now I want to create a widget on sidebar to display a list of the associations that have events associated with them. I also want to display the number of events associated near the name of every association. If I click on one of them, I want to go on an archive page with a list events only for that association.


Think of it like the standard archive plugin but with associations instead of months.


I don't have idea of which type of query I have to do inside the custom widget main function. This is my code at the moment:



private function getAssociationsList(){

// The Query
$associations = new WP_Query( array('post_type' => 'associations' ) );

if($associations->found_posts > 0) {
echo '<ul>';
while ($associations->have_posts()) {
$associations->the_post();

$events_query = array(
'post_type' => 'events',
'meta_query'=> array(
array(
'key' => 'mlw_event_association',
'value' => get_the_ID()
)
)
);

$events_for_this_association = new WP_Query($events_query);

if($events_for_this_association->found_posts > 0){
$listItem = '<li>';
$listItem .= '<a href="' . get_permalink() . '">';
$listItem .= get_the_title() . '</a>';
$listItem .= ' ('. $events_for_this_association->post_count . ')</li>';
echo $listItem;
}
}
echo '</ul>';
wp_reset_postdata();

}else{
echo '<p>_("No association with upcoming events at the moment")</p>';
}
}


The problem here is that get_permalink() is the link to the association page, not to a "filtered" archive page. Also with this query the tag is always shown, even if there aren't associations with events associated.


Any suggestion would be very appreciated.


Thank you!





Aucun commentaire:

Enregistrer un commentaire