$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>Show posts from custom post type sorted by categorytaxonomy on a one-pagerpage|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

Show posts from custom post type sorted by categorytaxonomy on a one-pagerpage

matteradmin10PV0评论

I'm working on a one-pager wp site for a festival (page: Home). This festival has 4 stages. I created a custom post type "artists" with custom taxonomy "stages" and entered the 4 names of the stages. (screenshot). I've added all artists and assigned them to a stage.

How can I show on my page an overview of stages with their artists?

Stage1: -Artist1 -Artist 2

Stage2: -Artist3 -Artist4

And so on ...

I've managed to show all artists but I can't figure out how to sort them by stage. I've tried hundreds of things without luck. This is my code so far ..

<?php 
            query_posts(array( 
                'post_type' => 'artists',
                'showposts' => -1,
                'order' => 'DESC',
                'orderby' => 'menu_order',
                'nopaging' => true,
                'ignore_sticky_posts' => true,
                'terms' => array(7, 10),//stage1&2
            ) );  
        ?>

        <?php while (have_posts()) : the_post(); ?>
            <a href="<?php the_field('artist_external_link'); ?>" target="_blank"><?php the_title(); ?></a>     
        <?php endwhile;?>

I appreaciate the help!

I'm working on a one-pager wp site for a festival (page: Home). This festival has 4 stages. I created a custom post type "artists" with custom taxonomy "stages" and entered the 4 names of the stages. (screenshot). I've added all artists and assigned them to a stage.

How can I show on my page an overview of stages with their artists?

Stage1: -Artist1 -Artist 2

Stage2: -Artist3 -Artist4

And so on ...

I've managed to show all artists but I can't figure out how to sort them by stage. I've tried hundreds of things without luck. This is my code so far ..

<?php 
            query_posts(array( 
                'post_type' => 'artists',
                'showposts' => -1,
                'order' => 'DESC',
                'orderby' => 'menu_order',
                'nopaging' => true,
                'ignore_sticky_posts' => true,
                'terms' => array(7, 10),//stage1&2
            ) );  
        ?>

        <?php while (have_posts()) : the_post(); ?>
            <a href="<?php the_field('artist_external_link'); ?>" target="_blank"><?php the_title(); ?></a>     
        <?php endwhile;?>

I appreaciate the help!

Share Improve this question asked Dec 16, 2018 at 13:49 fabrikgrafikfabrikgrafik 31 silver badge6 bronze badges 1
  • Get all taxonomy terms (stages), then get posts (artists) for each term (stage). – Max Yudin Commented Dec 16, 2018 at 15:05
Add a comment  | 

1 Answer 1

Reset to default 0

Thanks Max, this pointed me in the right direction. I found some code that worked and adapted this to my needs. It works like a charm but the only thing that still bothers me is that I can not change the order in which the stages appear. Order is probably defined by what "stage" added first to wordpress.

<?php 
            $terms_array = array( 
            'taxonomy' => 'stages', // you can change it according to your taxonomy
            'parent'   => 0 // If parent => 0 is passed, only top-level terms will be returned
            );
            $stages_terms = get_terms($terms_array); 
            foreach($stages_terms as $stage): ?>
            <div class="room">
                <h4><?php echo $stage->name; ?></h4>
            <?php 
            $post_args = array(
                'posts_per_page' => -1,
                'post_type' => 'artists', // you can change it according to your custom post type
                'tax_query' => array(
                    array(
                        'taxonomy' => 'stages', // you can change it according to your taxonomy
                        'field' => 'term_id', // this can be 'term_id', 'slug' & 'name'
                        'terms' => $stage->term_id,
                    )
                )
            );
            $myposts = get_posts($post_args); ?>
            <ul>
            <?php foreach ( $myposts as $post ) : setup_postdata( $post ); ?>
            <li>
                <a href="<?php the_field('artist_external_link'); ?>" target="_blank"><?php the_title(); ?></a>     
            </li>
            <?php endforeach; // Term Post foreach ?>
            </ul></div>
            <!-- end room -->   
            <?php wp_reset_postdata(); ?>

            <?php endforeach; // End Term foreach; ?>
Post a comment

comment list (0)

  1. No comments so far