I'm trying to display custom post types by excluding the sold ones but also by displaying them in alphabetical order from the county meta_key.
I'm trying to use pre_get_posts but I'm not sure that I doing it correctly
Here is my code on a page template:
$args = array(
'post_type' => 'property',
'orderby' => 'meta_value',
'meta_key' => 'random_775',
'order' => 'ASC',
'posts_per_page' => 100,
);
$view_all_properties_sorted = new WP_Query( $args );
and here is the function in my functions.php file:
add_action( 'pre_get_posts', 'sort_view_all_properties' );
function sort_view_all_properties($view_all_properties_sorted) {
$view_all_properties_sorted->set('meta_key', 'sold');
$view_all_properties_sorted->set('meta_value', 'true');
$view_all_properties_sorted->set('meta_compare', '!=');
}
When I use this code, I'm getting a 404 error when I add this line:
$view_all_properties_sorted->set('meta_key', 'sold');
Is it because meta_key has already been set? Any suggestions on what I may be doing wrong?