{"id":704,"date":"2022-08-30T15:05:47","date_gmt":"2022-08-30T15:05:47","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2013\/11\/09\/how-to-prevent-problem-with-alias-when-using-expose-field-in-hook_views_query_alter-collection-of-common-programming-errors\/"},"modified":"2022-08-30T15:05:47","modified_gmt":"2022-08-30T15:05:47","slug":"how-to-prevent-problem-with-alias-when-using-expose-field-in-hook_views_query_alter-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2022\/08\/30\/how-to-prevent-problem-with-alias-when-using-expose-field-in-hook_views_query_alter-collection-of-common-programming-errors\/","title":{"rendered":"How to prevent problem with alias when using expose field in hook_views_query_alter()?-Collection of common programming errors"},"content":{"rendered":"<p>I have some expose fields in my views and I used hook_views_query_alter for changing my sort and also I used my expose field alias in my query alter when user fills my expose field its alias will change in result my query does not work. How can I prevent changing my alias?<\/p>\n<p>This is the error I get:<\/p>\n<blockquote>\n<p>user warning: Unknown column &#8216;content_type_adver.field_adver_t_price_value&#8217; in &#8216;order clause&#8217; query: SELECT DISTINCT node.nid AS nid, node.title AS node_title, node.language AS node_language, node_data_field_adver_t_period.field_adver_star_count_value AS node_data_field_adver_t_period_field_adver_star_count_value, &#8230;&#8230;<\/p>\n<\/blockquote>\n<p>This is my code.<\/p>\n<pre><code>function mysort_views_query_alter(&amp;$view, &amp;$query) {    \n  if ($_GET[\"sortoption\"] &amp;&amp; $view-&gt;name == 'advers_list') {\n    $sort = check_plain(trim(strtolower($_GET[\"sortoption\"])));\n\n    switch ($sort) {\n      case \"posted-desc\":\n        $query-&gt;orderby[0] = 'node_changed DESC';\n        break;\n\n      case \"posted-asc\":\n        $query-&gt;orderby[0] = 'node_changed ASC';\n        break;\n\n      case \"price-asc\":\n        $query-&gt;orderby[0] = 'content_type_adver.field_adver_t_price_value ASC';\n        break;\n\n      case \"price-desc\":\n        $query-&gt;orderby[0] = 'content_type_adver.field_adver_t_price_value DESC';\n        break;              \n\n      case \"hotel-desc\":\n        $query-&gt;orderby[0] = 'node_data_field_adver_star_count_field_adver_t_hotel_value DESC';         \n        break;\n\n      case \"hotel-asc\":\n        $query-&gt;orderby[0] = 'node_data_field_adver_star_count_field_adver_t_hotel_value ASC';          \n        break;  \n\n      case \"end\":\n        $query-&gt;orderby[0] = 'node_title ASC';          \n        break;\n\n      case \"tripway\":\n        $query-&gt;orderby[0] = 'node_data_field_adver_star_count_field_adver_t_tripway_value ASC';            \n        break;                                                          \n    }\n\n    $query-&gt;orderby[1] = 'node_data_field_adver_star_count_field_adver_star_count_value DESC';\n    $query-&gt;orderby[2] = 'node_data_field_adver_t_price_exist_field_adver_t_price_exist_value DESC';\n    $query-&gt;orderby[3] = 'node_changed DESC';\n    $query-&gt;orderby[4] = '_random ASC';     \n  }  \n}\n<\/code><\/pre>\n<p id=\"rop\"><small>Originally posted 2013-11-09 21:42:09. <\/small><\/p>","protected":false},"excerpt":{"rendered":"<p>I have some expose fields in my views and I used hook_views_query_alter for changing my sort and also I used my expose field alias in my query alter when user fills my expose field its alias will change in result my query does not work. How can I prevent changing my alias? This is the [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-704","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/704","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=704"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/704\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=704"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=704"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=704"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}