仅供参考,我不是一个有经验的程序员,而且我对PHP的编码经验很少,可以肯定地说,我不知道基础知识,只是我在阅读、理解和编辑现有代码的过程中自学的内容。
我有一个网站,它有一个购买的主题,其中包括一个自定义的tax\\u查询,我复制了它的结构,以尝试按元数据中的不同字段过滤查询。我把我的函数放在函数中。我的子主题中的php文件,其中包含到目前为止我所做的所有修改。
这是我的代码:
function ah_meta_queries( $query = false ) {
if ( ! ( is_post_type_archive( \'listing\' ) || is_tax( \'listing_type\' ) || is_tax( \'listing_location\' ) ) ) return;
if ( is_admin() || ! is_a( $query, \'WP_Query\' ) || ! $query->is_main_query() ) return;
if ( is_post_type_archive( \'listing\' ) ) {
$meta_query_args = array( \'relation\' => \'AND\' );
// For each type of Meta Data, check if filtering is set, then add it to the query array
if ( isset( $_GET[\'et-listing-rating\'] ) && \'none\' != $_GET[\'et-listing-rating\'] ) {
$meta_query_args[] = array(
\'key\' => \'listing_type\',
\'value\' => intval( $_GET[\'et-listing-rating\'] ),
); }
if ( isset( $_GET[\'Fitness\'] ) )
$meta_query_args[] = array(
\'key\' => \'HN_Fitness\',
\'value\' => \'TRUE\',
);
if ( isset( $_GET[\'Furnished\'] ) )
$meta_query_args[] = array(
\'key\' => \'HN_Furnished\',
\'value\' => \'TRUE\',
);
if ( isset( $_GET[\'Kitchen\'] ) )
$meta_query_args[] = array(
\'key\' => \'HN_Kitchen\',
\'value\' => \'TRUE\',
);
if ( isset( $_GET[\'Parking\'] ) )
$meta_query_args[] = array(
\'key\' => \'HN_Parking\',
\'value\' => \'TRUE\',
);
if ( isset( $_GET[\'Pets Allowed\'] ) )
$meta_query_args[] = array(
\'key\' => \'HN_Pets\',
\'value\' => \'TRUE\',
);
if ( isset( $_GET[\'Pool\'] ) )
$meta_query_args[] = array(
\'key\' => \'HN_Pool\',
\'value\' => \'TRUE\',
);
// If user input Price-Low but no Price-High, make sure Listing Price Low or Listing Price High is more than Price-Low
if ( (isset( $_GET[\'price-low\'] ) && \'\' !=$_GET[\'price-low\'] ) && !isset( $_GET[\'price-high\']) )
$meta_query_args[] = array(
\'relation\' => \'OR\',
array(
\'key\' => \'HN_Price_Low\',
\'value\' => $_GET[\'price-low\'],
\'type\' => \'numeric\',
\'compare\' => \'>=\',
),
array(
\'key\' => \'HN_Price_High\',
\'value\' => $_GET[\'price-low\'],
\'type\' => \'numeric\',
\'compare\' => \'>=\',
),
);
// If user input Price-High but no Price-Low, make sure Listing Price Low or Listing Price High is less than Price-High
if ( !isset( $_GET[\'price-low\']) && (isset( $_GET[\'price-high\'] ) && \'\' !=$_GET[\'price-high\'] ) )
$meta_query_args[] = array(
\'relation\' => \'OR\',
array(
\'key\' => \'HN_Price_Low\',
\'value\' => $_GET[\'price-high\'],
\'type\' => \'numeric\',
\'compare\' => \'<=\',
),
array(
\'key\' => \'HN_Price_High\',
\'value\' => $_GET[\'price-high\'],
\'type\' => \'numeric\',
\'compare\' => \'<=\',
),
);
// If user input both Price-Low AND Price-High, make sure Listing Price Low or Listing Price High is BETWEEN Price-Low and Price-High
if ( (isset( $_GET[\'price-low\'] ) && \'\' !=$_GET[\'price-low\'] ) && (isset( $_GET[\'price-high\'] ) && \'\' !=$_GET[\'price-high\'] ) )
$meta_query_args[] = array(
\'relation\' => \'OR\',
array(
\'key\' => \'HN_Price_Low\',
\'value\' => array( $_GET[\'price-low\'], $_GET[\'price-high\'] ),
\'type\' => \'numeric\',
\'compare\' => \'BETWEEN\',
),
array(
\'key\' => \'HN_Price_High\',
\'value\' => array( $_GET[\'price-low\'], $_GET[\'price-high\'] ),
\'type\' => \'numeric\',
\'compare\' => \'BETWEEN\',
),
);
// If any of the filters are set in the URL, then run the Meta Query
if ( ( isset( $_GET[\'et-listing-rating\'] ) && \'none\' != $_GET[\'et-listing-rating\'] ) ||
isset( $_GET[\'Fitness\'] ) ||
isset( $_GET[\'Furnished\'] ) ||
isset( $_GET[\'Kitchen\'] ) ||
isset( $_GET[\'Parking\'] ) ||
isset( $_GET[\'Pets Allowed\'] ) ||
isset( $_GET[\'Pool\'] ) ||
( isset( $_GET[\'price-low\'] ) && \'\' !=$_GET[\'price-low\'] ) ||
( isset( $_GET[\'price-high\']) && \'\' !=$_GET[\'price-high\'] )
)
$meta_query = new WP_Meta_Query( $meta_query_args );
}
}
我的一些问题是:-为什么这个没有运行?是否必须将该函数设置为在其他任何地方运行-为什么函数是用参数($query=false)声明的?
欢迎提出任何建议!!