Forums Forums Search & Filter Pro WP_Query with meta_query with two clause and Search&Filter Pro

Viewing 5 posts - 1 through 5 (of 5 total)
  • Anonymous
    #191847

    I have custom post type job, which has one custom field chceckbox named top_job and it has only one choice Top job. There will be always only one Top job.

    I need to display this one top job always on first place (it has different grid size and styling) and the rest of jobs after that.

    I need to have only one while because I am using Search&Filter Pro plugin to filtering taxonomies – job type, area, city.

    I have this:

    $args = array(
      'search_filter_id' => 435,
      'post_type' => array('job'),
      'post_status' => array('publish'),
      'posts_per_page' => '-1',
      'meta_query' => array(
        'relation' => 'OR',
        'topjob_clause' => array (
          'key'   => 'top_job',
          'value'   => '"topjob"',
          'compare' => 'LIKE'
        ),
        'normaljob_clause' => array (
          'key'   => 'top_job',
          'value'   => '"topjob"',
          'compare' => 'NOT LIKE'
        )
      ),
      'orderby' => array(
        'topjob_clause' => 'DESC',
        'normaljob_clause' => 'DESC'
      ),
    );
    $query = new WP_Query( $args );
      if ( $query->have_posts() ) : ?>
        <div class="o-layout__item u-1/1">
          <?php echo do_shortcode('[searchandfilter id="435"]') ?>
        </div>
        <div class="job__filter">
        <?php while ( $query->have_posts() ) : $query->the_post();
          get_template_part('parts/list', 'job');
        endwhile; ?>
        </div>
      <?php else :
      _e('Sorry, there are no jobs yet.', 'lpw');
      endif;
    wp_reset_postdata();

    Orderby is fully ignored by the plugin. When I disable ‘search_filter_id’ => 435, in $args, orderby works like a charm …

    I don’t know where is the problem. Is the problem in my query or the plugin just don’t work with clauses?

    Trevor
    #191854

    You need to use our Edit Query Arguments filter instead:

    https://searchandfilter.com/documentation/action-filter-reference/#edit-query-arguments

    Anonymous
    #191856

    UPDATE:
    When I disable **’search_filter_id’ => 435,** in $args, orderby works partly … Top job is first, but other jobs are shown from oldest to newest and I need them from newest to oldest.

    Trevor
    #191864

    When you disable our argument, then your arguments are in place. As soon as you add our argument, it overrides the sort order, hence the need to use that filter I directed you to.

    You need a secondary sort order to do what you want.

    Anonymous
    #191910

    Thank you! That worked well! 🙂

Viewing 5 posts - 1 through 5 (of 5 total)