Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

Having troubles to get my taxonomy.php working good.

I use a taxonomy.php to list all my posts in a specific category but it shows every post I have in all categories.

Here is my code:

<ul class="scenes cf">

<?php

$args1 = array(
  'post_type'=>'artistes',
  'orderby' => 'menu_order',
  'order' => 'ASC',
  'meta_query' => array(
        array(
            'key' => 'date_de_lartiste',
            'value' => '',
            'compare' => 'LIKE'
        ),
        array(
            'key' => 'heure_de_lartiste',
            'value' => '',
            'compare' => 'LIKE'
        )
    )
);

add_filter('posts_orderby','orderbyreplace');
$loop = new WP_Query( $args1 );
remove_filter('posts_orderby','orderbyreplace');
?>
<?php $todaysDate = date('Y/m/d'); ?>
<?php if ($loop->have_posts() ) { while ( $loop->have_posts() ) { $loop->the_post(); ?>
<?php 
    $date = get_field('date_de_lartiste'); 
    $newDate = date("d/m/Y", strtotime($date));
?>
<li>
    <div class="info">
        <?php the_post_thumbnail( 'artists_list' )?>
        <p><?php the_title(); ?></p>
        <p class="music_style"><?php echo $newDate ?> - <?php the_field('heure_de_lartiste'); ?></p>
    </div>
    <a href="<?php the_permalink(); ?>" class="read_more"><?php _e('Read More'); ?></a>
</li>
<?php
    }
        } else {
            ?>
            <li class="cf">
                <p>No posts</p>
            </li>
            <?php
    }
        /* Restore original Post Data */
        wp_reset_postdata();
?>
</ul>

Does anyone may help me to figure it out? How to display only the post in the current taxonomy?

Thanks a lot,

Jhon

EDIT:

Well I know it's now the best way to do that, but I just fixed it with this code:

$category = get_query_var( 'term' );

And in my $args I added:

'tax_query' => array(

    array(

        'taxonomy' => 'scenes',

        'field' => 'slug',

        'terms' => "$category"

        )

    ),
share|improve this question
    
If you've fixed your problem can you post it as an answer rather than an edit? –  Tom J Nowell Mar 5 at 22:05
    
I can't within 10 hours! :) Not enough reputation. Thanks for your help –  jhon Mar 5 at 23:02

1 Answer 1

You haven't mentioned that you're trying to filter your taxonomy archives by meta key, but that's whats happening in your code.

The problem is that you're discarding the taxonomy query prepared for you ( bad! doubling the database work in the process ), and creating your own, but you never tell it which term to look for, WP_Query can't read minds, so it looks at all artistes, because you never told it which one to look for.

Instead of doing it the way you've done it, instead you should use the pre_get_posts filter to modify the query arguments before the main loop query happens. This way you don't need a second query, and no time is wasted

E.g. excluding a category:

// Load our function when hook is set
add_action( 'pre_get_posts', 'rc_modify_query_exclude_category' );

// Create a function to excplude some categories from the main query
function rc_modify_query_exclude_category( $query ) {

    // Check if on frontend and main query is modified
    if ( ! is_admin() && $query->is_main_query() && ! $query->get( 'cat' ) ) {
        $query->set( 'cat', '-5' );
    } // end if
}

Taken from remicorson.com

This way you can check if you're on the taxonomy page by looking at the $query object, and add the meta_query amongst other things. This also simplifies your taxonomy.php back to a standard post loop and halves the amount of database work needed.

share|improve this answer
    
Hey, I don't understand everything. Never used this way! And i'm not really confortable with multiple meta_key order. Can you please explain how to do that? Thanks a lot ! –  jhon Mar 5 at 21:58
    
multiple meta key order is a different, and new question, you should ask it in a new question –  Tom J Nowell Mar 5 at 22:05

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.