• I’ve been trying to create a list on the category page, listing all the posts with the “Main Image” custom field in that specific category and a list with the posts that don’t have the custom field “Main image”.

    So basically, two separate lists, listing the posts with the custom field and the ones without, in that specific category.

    Can anyone find a solution to this?

    Thanks!

Viewing 1 replies (of 1 total)
  • At it’s easiest something very similar to the count problem you posed yesterday:

    <?php
    $cat_name = 'Image';
    $custom_field = 'Main Image';
    $cat_id = get_cat_ID($cat_name);
    $args=array(
      'cat' => $cat_id,
      'post_type' => 'post',
      'post_status' => 'publish',
      'posts_per_page' => -1,
      'caller_get_posts'=> 1
    );
    $my_query = null;
    $my_query = new WP_Query($args);
    if( $my_query->have_posts() ) {
      echo 'List of Posts in category ' . $cat_name . ' with custom field ' . $custom_field ;
      while ($my_query->have_posts()) : $my_query->the_post();
    
        $image = get_post_meta($post->ID, $custom_field, true);
        if ($image){ ?>
        <p><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></p>
        <?php
        }
      endwhile;
    }
    if( $my_query->have_posts() ) {
      echo 'List of Posts in category ' . $cat_name . ' without the custom field ' . $custom_field;
      while ($my_query->have_posts()) : $my_query->the_post();
    
        $image = get_post_meta($post->ID, $custom_field, true);
        if (!$image){ ?>
        <p><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></p>
        <?php
        }
      endwhile;
    
    }
    wp_reset_query();  // Restore global post data stomped by the_post().
    ?>

Viewing 1 replies (of 1 total)
  • The topic ‘List posts without the custom field in one list, vice-versa.’ is closed to new replies.