WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have several "categories" and so called "projects" (post of content type "project"). To each project corresponds a category.

What I call a "gallery", is several images, with each image attached several strings to (description, author).

To each category and to each project should be attached a gallery. If to a project a gallery isn't attached, on the project page the gallery of the category should be shown.

(Note that what I call here "categories" may or may not be the same as what is called "categories" in the manuals of WordPress.)

How to do it in WordPress?

share|improve this question

If you plan on using WP's built-in image custom fields, include this query on your single-project.php (assuming galleries will only be called on single project pages):

// this will get all images attached to that specific post
$imagequery = new WP_Query(
    'post_type' => 'attachment',
    'post_mime_type' => 'image/jpeg',
    'post_parent' => $post->ID

if ($imagequery->have_posts()) {
    // loop to present those images here
else { // if no images attached to that post
    // fetch all posts from the taxonomy
    $catquery = get_posts(
        'post_type' => 'projects',
        'numberposts' => -1,
        'yourtaxonomyname' => 'yourtermname' // specify the 'category' here

    $postids = array();

    if ($catquery) {
        foreach ( $catquery as $onepost ) {
        array_push($postids, $onepost->ID); // make array of post ids

    $newimagequery = new WP_Query( // then query these ids to get their images
    'post_type' => 'attachment',
    'post_mime_type' => 'image/jpeg',
    'post__in' => $postids

if ($newimagequery->have_posts()) {
    // loop to present images from other posts here

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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