Custom post types archive

I re-wrote the function, taking a few liberties with it.

The main change was specify the ID of the association to link to in your call to get_permalink and to just use $association->ID instead of calling get_the_title (though you could just place the same ID in get the title).

I also switched from using the_post() to using next_post() which pulls the next post into a variable instead of stomping on the various post global variables.

Finally, since the function is getAssociationsList, I had it return the output instead of echoing it. (and I changed the name … as using underscores helps when people need to translate your function names a little)

Anywise, I hope this helps!

private function get_associations_list(){
    $associations = new WP_Query( array('post_type' => 'associations' ) );
    $output="";

    if( $associations->found_posts > 0 ) {
        $output .= '<ul>';
        while ($associations->have_posts()) {
            # changed to use next_post() so we can avoid messing with the global query
            # which avoids needing to use reset below.
            $association = $associations->next_post();

            # block this out as we no longer need it
            # $associations->the_post();

            # Changed value to use the objects ID rather than calling an extra function to get it
            $events_query = array(
                'post_type' => 'events',
                'meta_query'=> array(
                    array(
                        'key'   => 'mlw_event_association',
                        'value' => $association->ID
                    )
                )
            );

            # Checked via have_posts
            # - added target object to get permalink and title for
            # - changed count to use found_posts instead of post_count
            $events_for_this_association = new WP_Query($events_query);
            if ( $events_for_this_association->have_posts() ) {
                # There is at least one
                $association_link = get_permalink( $association->ID );
                $association_title = $association->post_title;
                $association_event_count = $events_for_this_association->found_posts;
                $output .= <<<HTML
<li><a href="https://wordpress.stackexchange.com/questions/176768/{$association_link}">{$association_title}</a> ({$association_event_count})</li>
HTML;

            }
        }
        $output .= '</ul>';

        # no longer needing to reset post data as we never used the global space
        # wp_reset_postdata(); 

    }else{
        $output="<p>" . __("No association with upcoming events at the moment") . '</p>';
    }

    # Maybe give caller option to echo it immediately instead of echoing it.
    return $output;
}