0

I am trying to show some posts on a template based on their custom taxonomy.

Here is my template code:

<?php $field = get_field('newsletter_date'); ?>
                <?php 
                $args = array (
                'orderby'                => 'rand',
                'taxonomy' => 'newsdates',
                'field' => 'id',
                'terms' => "$field",
                'post_type' => 'post',
                );

                $queryarticles = new WP_Query( $args ); ?>

                <?php while ($queryarticles -> have_posts()) : $queryarticles -> the_post(); ?>

                <h4 class="homerssfeed"><a href="<?php the_permalink() ?>" class="nound"><?php the_title(); ?></a></h4>

                <span class="dcecright">
                    <?php the_content(); ?>

                </span>
                <?php endwhile;?>
                <?php wp_reset_postdata(); ?>

and here is what is in my function.php:

// Register Custom Taxonomy
function newsdates_taxonomy()  {

    $labels = array(
        'name'                       => _x( 'Newsletter Dates', 'Taxonomy General Name', 'text_domain' ),
        'singular_name'              => _x( 'Newsletter Date', 'Taxonomy Singular Name', 'text_domain' ),
        'menu_name'                  => __( 'Newsletter Date', 'text_domain' ),
        'all_items'                  => __( 'All Newsletter Dates', 'text_domain' ),
        'parent_item'                => __( 'Parent Date', 'text_domain' ),
        'parent_item_colon'          => __( 'Parent Date:', 'text_domain' ),
        'new_item_name'              => __( 'New Date', 'text_domain' ),
        'add_new_item'               => __( 'Add Newsletter Date', 'text_domain' ),
        'edit_item'                  => __( 'Edit Date', 'text_domain' ),
        'update_item'                => __( 'Update Date', 'text_domain' ),
        'separate_items_with_commas' => __( 'Separate dates with commas', 'text_domain' ),
        'search_items'               => __( 'Search dates', 'text_domain' ),
        'add_or_remove_items'        => __( 'Add or remove dates', 'text_domain' ),
        'choose_from_most_used'      => __( 'Choose from the most used dates', 'text_domain' ),
    );
    $args = array(
        'labels'                     => $labels,
        'hierarchical'               => false,
        'public'                     => true,
        'show_ui'                    => true,
        'show_admin_column'          => true,
        'show_in_nav_menus'          => true,
        'show_tagcloud'              => true,
    );
    register_taxonomy( 'newsdates', 'post', $args );

}

// Hook into the 'init' action
add_action( 'init', 'newsdates_taxonomy', 0 );

}

1 Answer 1

0

You are setting your taxonomy arguments incorrectly in WP_Query. You need to set up a tax_query, which takes an array of arrays. See the codex.

$args = array(
    'post_type' => 'post',
        'orderby' => 'rand',
    'tax_query' => array(
        array(
            'taxonomy' => 'newsdates',
            'field' => 'id',
            'terms' => $field
        )
    )
);
1
  • Vancoder you are awesome! Thanks for your quick response.
    – RussC
    Aug 21, 2013 at 4:03

Your Answer

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

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