0
votes

Been stuck on this for days, so I thought I'd reach out.

I've created two custom taxonomies, singleclient and dualclient. These are populated with special content via Advanced Custom Fields.

What I'm looking to do is create a custom page template which joins these two taxonomies and prints out content from the two custom taxonomies.

This works for me for one of the taxonomies:

<?php 
                $args=array(
                  'post_type' => 'singleclients',
                  'posts_per_page' => -1,
                  'caller_get_posts'=> 1
                );
                $my_query = null;
                $my_query = new WP_Query($args);
                if( $my_query->have_posts() ) {

                  while ($my_query->have_posts()) : $my_query->the_post(); ?>
                    <?php the_field('client1_img'); ?>
                    <h3><?php the_field('client1_name'); ?></h3>

                <?php
                  endwhile;
                }
                wp_reset_query();  // Restore global post data stomped by the_post().

            ?>

I've tried following what the Wordpress Codex says to do, but I can't seem to get it working. Here's an example which I thought would work, but displays nothing at all.

<?php 

                $args=array(
                    'post_type' => 'post',
                    'tax_query' => array(
                        'relation' => 'AND',
                        array(
                            'taxonomy' => 'singleclients',
                            'field'    => 'slug',
                            'terms'    => 'singleclients'
                        ),
                        array(
                            'taxonomy' => 'dualclients',
                            'field'    => 'slug',
                            'terms'    => 'dualclients'
                        )
                    )
                );
                $posts = get_posts( $args );

                $my_query = null;
                $my_query = new WP_Query($args);
                if( $my_query->have_posts() ) {

                  while ($my_query->have_posts()) : $my_query->the_post(); ?>
                    <?php the_field('client1_img'); ?>
                    <p>print test content</p>
                    <h3><?php the_field('client1_name'); ?></h3>

                <?php
                  endwhile;
                }
                wp_reset_query();  // Restore global post data stomped by the_post().

            ?>

Really pretty frustrated at this point. Anyone know what I am doing wrong and why it won't print out both taxonomies?

3

3 Answers

0
votes

I did this on a website a while back:

//queries
$query1 = new WP_Query($arg1);
$query2 = new WP_Query($arg2);

//create new empty query
$wp_query = new WP_Query();
$wp_query->posts = array_merge( $query1->posts, $query2->posts );

You can now access your posts as you normally would. I am not very good at explaining stuff, so let me know if you need more info...

0
votes

I couldn't get this to work, unfortunately.

What I ended up doing was creating one single taxonomy called "clients" and enabling categories for that taxonomy. This allowed me to assign "children" to clients of the types "singleclient" and "dualclient".

When I called the loop, I called one single taxonomy and then separated the output posts into two types. My code is below. I hope that this helps someone in the future.

<?php
                $args=array(
                  'post_type' => 'clients',
                  'posts_per_page' => -1,
                  'caller_get_posts'=> 1
                );
                $my_query = null;
                $my_query = new WP_Query($args);
                if( $my_query->have_posts() ) {

                  while ($my_query->have_posts()) : $my_query->the_post(); ?>

                    <?php if ( in_category( 'dualclient' )) { ?>
                        <!-- show dual client -->
                        <section class="two columns padding10">
                            <a href="<?php the_permalink() ?>">
                                <section class="four columns client1 dualclient_first" style="background-image:url('<?php the_field("dualclient1_img") ?>')"></section>
                                <section class="four columns client1" style="background-image:url('<?php the_field("dualclient2_img") ?>')"></section>
                            </a>
                        </section>
                    <?php } elseif ( in_category( 'singleclient' )) { ?>
                        <!-- show single client -->
                        <section class="two columns padding10">
                            <a href="<?php the_permalink() ?>">
                                <section class="eight columns client1" style="background-image:url('<?php the_field("dualclient1_img") ?>')"></section>        
                            </a>
                        </section>
                    <?php } else { ?>
                        <p>Woops.</p>
                    <?php } ?>

                <?php
                  endwhile;
                }
                wp_reset_query();  // Restore global post data stomped by the_post().
            ?>
-1
votes

Have you tried this way?

//First Query
$args=array(
   'taxonomy' => 'singleclients',
   'posts_per_page' => 1,
);
query_posts($args);

//Get the Posts
get_template_part( 'loop', 'index' );
wp_reset_query();

//Second Query
$args=array(
   'taxonomy' => 'dualclients',
   'posts_per_page' => 1,
);
query_posts($args);

wp_reset_query();