0

I am having a query like this

Code 1

$term = get_term_by( 'slug', get_query_var( 'term' ), get_query_var( 'taxonomy' ) );
$args = array(
    'post_type'              => 'l',
    'post_status'            => array( 'published' ),
    'author_in'              => $authors,
    'posts_per_page'         => '1',
    'order'                  => 'ASC',
    'orderby'                => 'rand',
    'tax_query' => array(
array (
        'taxonomy'      => $term->taxonomy,
        'field'         => $term->slug,
        'terms'         => $term->term_id,

)
));

It just gives me one post . But if i use like below it works but i can't sort it by taxonomy terms.

Code 2

$term = get_term_by( 'slug', get_query_var( 'term' ), get_query_var( 'taxonomy' ) );
$args = array(
    'post_type'              => 'l',
    'post_status'            => array( 'published' ),
    'author_in'              => $authors,
    'posts_per_page'         => '1',
    'order'                  => 'ASC',
    'orderby'                => 'rand',
   );

I am using this is a template file :taxonomy-work.php.

I want it to make code 1 work but it rand doesn't seems to work unless i remove tax_query

Can it be done this way or do i have to look for something else? Thanks

2

You have syntax error in tax_query.

'tax_query' => array(
    array (
        'taxonomy'      => $term->taxonomy,
        'field'         => 'term_id', // this is default value, so can be ommited
        'terms'         => $term->term_id,
    )
)

As you can read in Codex, possible values for field parameter are: term_id (default), name, slug or term_taxonomy_id.

In this question you can read about another method of getting a random post.

  • oh. I was working on my project with this error for more than a month.Thanks for clearing out :) – Latheesh V M Villa Aug 30 '18 at 20:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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