I am struggling to know how I can use pre_get_post
when the submit’s metadata is just not saved within the wp_postmeta desk. I exploit Pods (https://pods.io) to create customized submit sorts with customized fields, saved utilizing the plugins Desk Storage choice.
My desired end result is to make use of orderby
on the meta_value
saved within the customized xyz_custom_table
desk.
The next xyz_order_post_by_meta
operate is how I’d do that with posts that retailer metadata within the wp_postmeta desk. However this won’t work with customized database tables.
operate xyz_order_post_by_meta($question)
{
if (
$query->is_main_query() && !$query->is_feed() && !is_admin() && $query->is_post_type_archive("xyz_cpt")
) {
$query->set("meta_key", "xyz_custom_meta");
$query->set("orderby", "meta_value");
$query->set("order", "ASC");
}
}
add_action("pre_get_posts", "xyz_order_post_by_meta", 9999);
Am I proper in pondering I have to run a customized SQL question to get the worth for meta_value
to incorporate within the above? If that’s the case, how would I write this? I’ve tried the next, which returns a listing of the meta_value
for every submit. However I am unsure how you can write this because the array I believe I want for $query->set("orderby", "meta_value");
to provide me the specified end result?
world $wpdb;
$table_name = "xyz_custom_table";
$retrieve_data = $wpdb->get_results("SELECT * FROM $table_name");
foreach ($retrieve_data as $retrieved_data) {
echo $retrieved_data->xyz_custom_meta;
}