2

I need to do something like that:

        $postsOrder = get_sub_field('posts-ordering');

        if ($postsOrder = 'post_views_count') {
            $queryPopular = array (
                'meta_key' => 'post_views_count',
                'orderby' => 'meta_value_num',
            );
        }

        $query = new WP_Query(
            array(
            'posts_per_page' => $postsCount,
            'post_type' => $postsType->name,
            'order' => 'DESC',
            'orderby' => $postsOrder,
            'taxonomy' => $postsTaxonomy,
            $queryPopular
            ),
        );

The point is that if $postsOrder is equal 'post_views_count', then in $query should be added two another parameters. How to do it right?

0

You can use below code to add other two parameter as condition.

  $args = array(
      'posts_per_page' => $postsCount,
      'post_type' => $postsType->name,
      'order' => 'DESC',
      'orderby' => $postsOrder,
      'taxonomy' => $postsTaxonomy,
  );

  $postsOrder = get_sub_field('posts-ordering');

  if ($postsOrder == 'post_views_count') {
      $args['meta_key'] = 'post_views_count';
      $args['orderby'] = 'meta_value_num';
  }
  $query = new WP_Query($args);
  • Can somebody tell me whats wrong in above code ? why answer was downvoted ? – Chetan Vaghela Dec 9 '19 at 12:48
  • 1
    Thanks! I'll check it soon :) Don't worry about downvoted answer, there is a lot of strange behaviors on Stack ;) – Damian P. Dec 9 '19 at 23:23

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.