0

My company is working on a project that will have to display Events based on a the Office they are located in and the Event Leader associated with that Office.

What we need is to build a system that allows Events, Offices and Event Leaders to be custom post types with an inheritance between them.

To display them, I think it would be easiest to fill an array with the relevant post types and then loop through the array to display them. I've never heard of this being done though. Is it possible to add Custom Post Types to an array?

EDIT: We're looking to display post types from an array because we are only showing posts based on the proximity to a certain zip code. The thought was that we would loop through the database and pull out the relevant post types and store them in an array so that we are able to display only the relevant post types on the fly.

0

You just need to use get_post_types() and loop through foreach to get all registered post type.

$post_types = get_post_types( '', 'names' ); 

foreach ( $post_types as $post_type ) {
    echo '<p>' . $post_type . '</p>';
}

See details in WordPress Codex https://codex.wordpress.org/Function_Reference/get_post_types

1
  • Thanks for your comment. The original question may not have been clear enough. Please see the edit. Jul 9 '15 at 12:49
0

Your way of expressing yourself is confusing, probably because you don't quite master the terms you are using but it's OK.

What you mean to say is that you need to fetch several posts with different post type formats. I would just use meta data to link am Event Leaders post to the other two post types. Or there is this plugin called Posts 2 Posts that might help you.

And yes, there is nothing special about constructing an array of posts. But really, this is such a broad topic with so many variables in it (how do you structure your templates, how do you build the necessary information / criteria to fetch the posts) that you should really be more specific about (any) issues you are facing.

0

Try this:

$post_types = array("partner", "post"); // List the post types that you want to query
foreach ($post_types as $post_type) {

echo '<p>' . $post_type . '</p>';

$query = new WP_Query(array(
    'posts_per_page' => 3,
    'post_type' => $post_type
        )
);
// The Loop
if ($query->have_posts()) {
    echo '<ul>';
    while ($query->have_posts()) {
        $query->the_post();
        echo '<li>' . get_the_title() . '</li>';
    }
    echo '</ul>';
} else {
    // no posts found
}
/* Restore original Post Data */
wp_reset_postdata();
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.