0
votes

I've got a custom post type that I've sorted by date from an ACF date picker field. I need to be able to filter these results based on a custom taxonomy that I've got. I can't seem to work out where to add the tax_query array. Keeps breaking the site. Taxonomy name is 'pre_job_status', term to filter by is 'survey-complete' Currently I have the following code

<?php 

// get posts
$posts = get_posts(
'tax_query' => array(
        array(
            'taxonomy' => 'pre_job_status',
            'field' => 'slug',
            'terms' => array( 'survey-complete' )
        ),
    ),
array(
    'post_type'         => 'pre_jobs',
    'posts_per_page'        => -1,
    'meta_key'          => 'survey_date',
    'orderby'           => 'meta_value',
    'order'             => 'ASC',
));

if( $posts ): ?>
    <hr>
    <ul>

    <?php foreach( $posts as $post ): 

        setup_postdata( $post )

        ?>
        <?php $requestor = get_field('pre_job_requestor', $client->ID ); ?>
        <?php $survey_site = get_field('survey_site', $client->ID ); ?>
        <li>
            <?php the_field('survey_date'); ?> - <a href="<?php the_permalink(); ?>"><?php the_title(); ?> - <?php echo $requestor[0]->post_title; ?> - <?php echo $survey_site[0]->post_title; ?></a>
        </li><hr>

    <?php endforeach; ?>

    </ul>

    <?php wp_reset_postdata(); ?>

<?php endif; ?>
1
Can you post your query with the tax_query?Howard E
Edited code with tax_query inCharlie McCluskey

1 Answers

1
votes

Your get_posts() query should look like the following:

// get posts
$posts = get_posts(array(
    'post_type'         => 'pre_jobs',
    'posts_per_page'        => -1,
    'meta_key'          => 'survey_date',
    'orderby'           => 'meta_value',
    'order'             => 'ASC',
    'tax_query' => array(
        array(
            'taxonomy' => 'pre_job_status',
            'field' => 'slug',
            'terms' => array( 'survey-complete' )
        ),
    ),
));

The tax_query array should be added to the main query array.