WordPress: FacetWP query orderby array settings not applied on initial page load

181 Views Asked by At

I am trying to use a custom sortby option with an array.

I edited the query in FacetWP Settings -> Listings in the Query Tab in Dev mode so as to define an array for the orderby parameter of my archive query.

I was expecting this to affect the query on initial load of documents. It seems to only affect it on filter via a Facet.

Here is my code for the query:

     <?php
    return [
       "post_type" => "document",
       "post_status" => "publish",
       "posts_per_page" => 12,
       'meta_key'          => 'annee',
       'orderby'           => [
                 'meta_value' => 'DESC',
                 'post_date' => 'DESC',
            ], 
    ];

When I first load the page on the front end, my sortby settings are not being used for some reason. See console debug output:

    FWP.settings.debug.query_args.orderby
    "meta_value"

However, if I click on any facet (and reload the data) then they are applied correctly:

    FWP.settings.debug.query_args.orderby
    Object { meta_value: "DESC", post_date: "DESC" }

Any ideas why my settings are not used on initial load of the page? How can I fix this?

1

There are 1 best solutions below

0
Robin Darlington On

I found the solution myself. I had a function that was setting the query parameters on initial load like so. Once I updated it, it worked as expected. Here is the updated version :

function custom_query_vars( $query ) {
    if ( !is_admin() && $query->is_main_query() ) {
      if ( is_post_type_archive('document') ) {
          $query->set( 'meta_key', 'annee' );
          $query->set( 'orderby',  [
             'meta_value' => 'DESC',
             'post_date' => 'DESC',
        ] );
          $query->set( 'order', 'DESC' );
      }

    }
    return $query;
  }