0

I'm trying to figure out how to query posts on a custom post type, using a custom field.

Goal: On my "artists" single page, I want to display a list of news/album releases for that artist only. Those posts will also show up on the main page.

Concept:
Use a category for each artist on the posts; use a custom field "news_category_slug" on each artist page to link them. I then want to query posts and display all posts in the category that match the custom value "news_category_slug".

Maybe I'm going about this the wrong way; I don't have much PHP knowledge so I'm having trouble figuring out if this is a bad approach or if I just don't understand how to nest a variable inside the query! Help!

1
  • why not use a single artist taxonomy for both? an artist post has its own artist term, and their news posts have that same term to connect them to the artist.
    – Milo
    Dec 28 '17 at 21:39
0

If you are creating artists as categories then their single page template will be category.php , in category.php add the following code :-

$category_id = get_cat_id( single_cat_title("",false) ); $cat_name = get_cat_name($category_id); $new=query_posts('post_type=news_album&cat='.$category_id.'&post_status=publish&orderby=ID&order=DESC&posts_per_page=-1'); if( have_posts()) : while(have_posts()) : the_post(); echo get_the_title($post->ID); echo get_the_content($post->ID);

endwhile; else: echo "No posts added for this category!"; endif; wp_reset_query();

0

if you use CPT then you should something like this:

<?php 
            query_posts(array( 
                'post_type' => 'artists',
                'showposts' => 10,
                'tax_query' => array(
                           array (
                             'taxonomy' => 'category_custom_post_type', // <= Name of the taxonomy created type category for the CPT
                             'field' => 'slug',
                             'terms' => 'news_category_slug', // <= Slug taxonomy
                          )
            ),
            ) );  
        ?>
        <?php while (have_posts()) : the_post(); ?>
                <h2><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></h2>
                <p><?php echo get_the_excerpt(); ?></p>
        <?php endwhile;  
    // reset the orignal query we should use this to reset wp_query  
    wp_reset_query(); ?>

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.