0

I am trying to get 3 pages, Sales, Lettings and Agricultural to show properties from their custom taxonomy. Lettings and Agricultural work fine, but for some reason Sales is showing properties from all categories.

I've tried everything I can think of and it doesn't seem to be working so i wondered if anybody here would have any ideas? Please see the code below with the WPQuery.

I've checked in my WP install and properties are definitely showing in Sales that don't have the sales custom taxonomy type. Yet the code is the same for Lettings and Agricultural and they work fine.

Also on Sales the pagination seems to be broken, showing 9 possible pages, then when i click page 2 it drops to 4 pages. Again it's fine on the other categories.

I really appreciate your help in advance! Thanks!

$host = $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
if($host == 'www.website.co.uk/status/lettings/') {
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
$query = new WP_Query( array(
    'post_type'         => 'properties',
    'posts_per_page'    => 12,
    'meta_key'          => 'sale_price',
    'orderby'           => 'meta_value_num',
    'order'             => 'DESC',
    'paged' => $paged,
    'tax_query' => array(
        array(
            'taxonomy' => 'statuses',
            'terms' => '48',
        )
    )
) );
}else if ($host == 'www.website.co.uk/status/sales/') {
    $paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
    $query = new WP_Query( array(
        'post_type'         => 'properties',
        'posts_per_page'    => 12,
        'meta_key'          => 'sale_price',
        'orderby'           => 'meta_value_num',
        'order'             => 'DESC',
        'paged' => $paged,
        'tax_query' => array(
            array(
                'taxonomy' => 'statuses',
                'terms' => '74',
            )
        )
    ) );
}else{
    $paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
    $query = new WP_Query( array(
        'post_type'         => 'properties',
        'posts_per_page'    => 12,
        'meta_key'          => 'sale_price',
        'orderby'           => 'meta_value_num',
        'order'             => 'DESC',
        'paged' => $paged,
        'tax_query' => array(
            array(
                'taxonomy' => 'statuses',
                'terms' => '52',
            )
        )
    ) );    
}
  • 1
    OK, so you're trying to use pages as categories. Why not to use just categories instead? What are the taxonomies and CPTs, and what terms are added to that taxonomies? (what are terms 52, 74, 48?) – Krzysiek Dróżdż Jun 25 '18 at 14:05
  • Hi @KrzysiekDróżdż. So i've got a category page that is taxonomy-statuses.php. I'm using advanced custom fields for the sale_price field, and the client wants to order the properties by highest price, which is why i've had to do this convoluted code. I feel like the code i've got here might be too much for what i'm trying to do. 52, 74 and 48 are lettings sales and agricultural and they are all under the 'statuses' taxonomy. – reigns1989 Jun 25 '18 at 14:32
  • 1
    You can modify the default taxonomy pages with pre_get_posts without having to create these new pages with custom queries. – Milo Jun 25 '18 at 16:28
  • 1
    I feel like you shouldn't be checking against the full URL for conditionals but either is_page() or some other WordPress Conditional Function. At the very least get_query_var(). – Howdy_McGee Jun 25 '18 at 17:06
  • @AdamC OK, so all this code is just to sort posts by a custom field? – Krzysiek Dróżdż Jun 25 '18 at 17:23
0

It was a little bit hard to understand the question at first, but... If all you want to do is to sort properties by some custom field, then there's no need to create custom WP_Queries and all that code you wrote...

All you need to do is to add sorting params to the main query using pre_get_posts action:

function sort_properties_by_custom_field( $query ) {
    if ( ! is_admin() && array_key_exists('post_type', $query->query_vars) && 'properties' == $query->query_vars['post_type'] ) {
        $query->set( 'meta_key', 'sale_price' );
        $query->set( 'orderby', 'meta_value_num' );
        $query->set( 'order', 'DESC' );
    }
}
add_action( 'pre_get_posts', 'sort_properties_by_custom_field' );

It will make all the loops showing properties on the front-end sorted by sale_price. If you want to sort them this way only in these categories, then you'll have to change the condition to:

if ( ! is_admin() && $query->is_main_query() && is_tax('statuses') ) {
| improve this answer | |
  • I actually did this myself slightly different to what you did, just came back to let you know i solved it. – reigns1989 Jun 26 '18 at 10:34
  • 1
    function my_pre_get_posts( $query ) { // do not modify queries in the admin if( is_admin() ) { return $query; } // only modify queries for 'event' post type if( $query->is_tax('statuses') ) { $query->is_main_query(); $query->set('orderby', 'meta_value_num'); $query->set('meta_key', 'sale_price'); $query->set('order', 'DESC'); } // return return $query; } add_action('pre_get_posts', 'my_pre_get_posts'); – reigns1989 Jun 26 '18 at 10:35
  • @AdamC You don't need to return anything in this action - so this part // return return $query; is redundant :) – Krzysiek Dróżdż Jun 26 '18 at 10:36

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.