First of all, I appologize in advance for my ignorance. I am trying to re-order a listing plugin’s WP_Query by the id of a meta value called "_fm_plans" using the ‘pre_get_posts’ action in functions.php. The original query looks liek this in the plugin’s folder:
$args = array(
'post_type' => ATBDP_POST_TYPE,
'posts_per_page' => -1,
'post_status' => 'public',
'meta_query' => array(
'relation' => 'AND',
array(
'key' => '_listing_status',
'value' => 'post_status',
),
array(
'key' => '_featured',
'value' => 1,
),
),
);
$listings = new WP_Query( $args );
I also see it defined as ‘at_biz_dir’ here:
if ( !defined('ATBDP_POST_TYPE') ) { define('ATBDP_POST_TYPE', 'at_biz_dir'); }
I AM able to spit out the values I’m looking for in part of a template file by over-riding it (similar to WooCommerce) to create custom ribbons for each listing plan type:
$listings->loop_thumb_card_template();
$listings->render_loop_fields($loop_fields['thumbnail']['avatar']);
/* START PA CUSTOM EDITS */
$plan_id = get_post_meta( get_the_ID(), '_fm_plans', true );
if ($plan_id) {
if ($plan_id == "13713") { $list_class = "gold";}
if ($plan_id == "13714") { $list_class = "silver";}
if ($plan_id == "13715") { $list_class = "bronze";}
if ($plan_id == "13716") { $list_class = "business";}
} else {
$plan_id = "";
$list_class = "";
}
?>
<div class="<? echo($plan_id . " " . $list_class); ?> ribbon"><? echo($list_class); ?></div>
<? /* END PA CUSTOM EDITS */ ?>
The edits are between the "PA CUSTOM EDIT" comments, but I want to have this custom post type query sort by that meta value so that Gold is first, followed by silver, and so on.
Here is what I’ve tried. All I get is a 404 page not found error.
// Function accepting current query
function my_change_order( $listings ) {
// Check if the query is for an archive
//if($listings) {
// Query was for archive, then set order
$listings->set( 'post_type', ATBDP_POST_TYPE );
$listings->set( 'orderby' , 'meta_value' );
$listings->set( 'meta_key' , '_fm_plans' );
$listings->set( 'order', 'DESC' );
//} else {
// Return the original query for non-archive pages
return $listings;
//}
// Return the query
return $listings;
}
// Runs before the posts are fetched
add_filter( 'pre_get_posts' , 'my_change_order' );
I have also tried adding quotes to "ATBDP_POST_TYPE", and replacing it with "at_biz_dir". Any help would be greatly appreciated.
2
Answers
This is what ended up working for me.
Did you check the PHP error log or the WordPress debug log for any messages?
What happens if you update your
my_change_order
function with this?