0

For all of my pages I have setup a taxonomy field using the plugin Advanced Custom Fields, for which I can then select as Home category, or something else like News. I am using the default category option, not a custom taxonomy.

I have a custom post type slides_post_type where for each new slide I am selecting the relevant category, i.e. Home.

The code I am using at present to output the slides is:

<?php
$slides_category = get_field('slider_category');
$args = array(-
'post_type' => 'slides_post_type',
'tax_query' => array(
    array(
        'taxonomy' => 'category',
        'field'    => 'slug',   
        'terms'    => array( $slides_category ),
    ),
),    
);
$query = new WP_Query( $args );
$my_secondary_loop = new WP_Query( $args );
if( $my_secondary_loop->have_posts() ): while( $my_secondary_loop->have_posts() ): $my_secondary_loop->the_post(); ?>
    //Post content goes here
<?php endwhile; ?>

This should be that where my page is set to Home and my slides are set to the same category it should show those slides. However this code is not outputting anything.

Any suggestions?

3
  • Is your taxonomy field storing slugs? Also you're creating 2 new queries but only using one, I suggest reading WP_Query codex page thoroughly.
    – Milo
    Nov 13, 2014 at 14:35
  • Is the - in this line $args = array(- just a typo. Also, the value stored in slider_category, does that match the slug or name of the category. If the value in your field matches the name of the category, your query won't work Nov 13, 2014 at 17:47
  • The extra '-' wad a mistake and I changed the field value to 'id' and it worked. Thank you all for your help. Nov 13, 2014 at 23:02

2 Answers 2

0

The final code I used was:

<?php
$slides_category = get_field('slider_category');
$args = array(
    'post_type' => 'slides_post_type',
    'tax_query' => array(
        array(
            'taxonomy' => 'category',
            'field'    => 'id',
            'terms'    => array( $slides_category ),
        ),
    ),
);
$slides_loop = new WP_Query( $args );
if( $slides_loop->have_posts() ): while( $slides_loop->have_posts() ): $slides_loop->the_post(); ?>
     // Post content goes here
<?php endwhile; ?>
-2

Use wp_reset_query() after first query, on 3th code line $args = array(- clear extra -

2
  • Please properly format your code, and explain why this would help the OP solve his problem Nov 13, 2014 at 15:41
  • Thank you Computer Information but having tried your solution it did not work, and although I am no real coder I cannot see how inserting the reset command after the first query would make much of a difference. Nov 13, 2014 at 17:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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