0

Okay, i'll try to go a bit more into detail. What i did with this code is getting a list of artists ordered by alphabet. In this list i'm showing all corresponding news posts to this artists term. That functions fine. But if there is a news that is corresponding to more than one of the artists i get the same post under every artist corresponded to the post. And what i wanna achieve is to show this post/news only ones for the first artist that is corresponded to this post. Instead of showing the post several times for every artist in the list. The shown code works for the listing of the artists and showing the corresponding posts beneath. Thanks in advance and sorry for the previous shaky description.

<?php foreach ( $artist_terms as $artist_term ): ?>

    <?php $news = get_posts(array (
    'numberposts' => 0, 
    'order' => 'DESC', 
    'meta_key' => 'date', 
    'value' => 'date(yyyy-mm-dd)', 
    'orderby' => 'meta_value',  
    'post_type' => 'news', 
    'artist' => $artist_term->slug
    ));
    ?>

    <? if($news): ?>

        <li>
            <?php foreach ($news as $post) : ?>

                        <div>   
                            <?php the_post_thumbnail();?>
                            <?php $artist_name = strip_tags (get_the_term_list( get_the_ID(), 'artist')); echo $artist_name; ?>
                        </div>      

            <?php endforeach; ?>        
        </li>

    <?php endif; ?>
<?php endforeach; ?>

alternative to this code i found this one http://wordimpress.com/loop-through-categories-and-display-posts-within/. That does the same but also has no solution for the duplicates.

<?php
/*
 * Loop through Categories and Display Posts within
 */
$post_type = 'news';

// Get all the taxonomies for this post type
$taxonomies = get_object_taxonomies( array( 'post_type' => $post_type ) );

foreach( $taxonomies as $taxonomy ) :

    // Gets every "category" (term) in this taxonomy to get the respective posts
    $terms = get_terms( $taxonomy );

    foreach( $terms as $term ) : ?>

      <?php// echo $term->name; ?>

     <?php $ids = array(); 
         $ids[] = get_the_ID();
     ?>

        <?php
        $args = array(
                'post_type' => $post_type,
                'posts_per_page' => -1,  //show all posts
                'tax_query' => array(
                    array(
                        'taxonomy' => $taxonomy,
                        'field' => 'slug',
                        'terms' => $term->slug,
                        'post__not_in' => $ids
                    )
                )

            );
        $posts = new WP_Query($args);

        if( $posts->have_posts() ): while( $posts->have_posts() ) : $posts->the_post(); ?>

            <?php
        $args2 = array(
                'post_type' => $post_type,
                'posts_per_page' => -1,  //show all posts
                'tax_query' => array(
                    array(
                        'taxonomy' => $taxonomy,
                        'field' => 'slug',
                        'terms' => $term->slug,
                        'post__not_in' => $ids
                    )
                )

            );
        $posts2 = new WP_Query($args2); ?>



                    <?php if(has_post_thumbnail()) { ?>
                            <?php the_post_thumbnail(); ?>
                            <br>

                    <?php }
                    /* no post image so show a default img */
                    else { ?>
                           <img src="<?php bloginfo('template_url'); ?>/assets/img/default-img.png" alt="<?php echo get_the_title(); ?>" title="<?php echo get_the_title(); ?>" width="110" height="110" />
                    <?php } ?>
                    <br>
                   <?php  echo get_the_title(); ?>
                   <br><br><br>


        <?php endwhile; endif; ?>

    <?php endforeach;

endforeach; ?>
0

value is not a valid parameter for get_posts and the tax => slug meta query pattern is long since deprecated, as has been numberposts. Even if value was valid it is hard to see how passing a literal date(yyyy-mm-dd) to it would work.

At any rate, I believe that applying a posts_groupby filter would do this. From the Codex:

add_filter( 'posts_groupby', 'my_posts_groupby' );
function my_posts_groupby($groupby) {
    global $wpdb;
    $groupby = "{$wpdb->posts}.ID";
    return $groupby;
}

That will force posts to only show up in once in the results.

  • Thanks for the hint and also making me aware of the other stuff going on in my code. I tried to get this to work and went through the codex. But didn't really achieve what i wanted. So I'm not sure if i'm using it right. Do you have any hint on how i would go about to apply this to my code. Your help is really appreciated. – Jay Kraist Jan 24 '14 at 11:23
  • Right now, most of what I am doing is guessing based on the code, but if the code is badly wrong my guesses may be badly off. Edit your question to include a detailed explanation of what you want to achieve in English. Leave all relevant code, but explain in words as well. – s_ha_dum Jan 24 '14 at 13:51
  • So the code i was posting is working for most of the part. It's showing a list of posts by the assigned artist (taxonomy/term). But when i assign more than one artist to a post, the post shows up several times. What i try to achieve but couldn't was to only show the post with several artists assigned once for the first artist it shows up for. – Jay Kraist Jan 27 '14 at 11:54

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.