カスタム投稿タイプ (曲) とそれに関連付けられたいくつかのカスタム フィールドがあります。カスタム フィールドの 1 つはチェックボックス (sample_playlist) で、残りはテキスト文字列です。
select 要素を追加し、チェックボックスの値を使用して、edit.php?post_type=songs で結果をフィルタリングしています。
add_filter( 'parse_query', array( &$this, 'wpse45436_posts_filter' ) );
function wpse45436_posts_filter( $query ){
// current page and post type checks omitted
$query->query_vars['meta_key'] = 'sample_playlist';
$query->query_vars['meta_value'] = 'on'; //these are the queries that get executed when filtering the posts that have the custom field checkbox checked
}
チェックボックスの値に基づいて結果をフィルタリングしようとすると、これはうまく機能します。
同じページで他のカスタム フィールド値による検索も可能です。
add_filter( 'posts_join', array( &$this, 'songs_search_join' ) );
add_filter( 'posts_where', array( &$this, 'songs_search_where' ) );
function songs_search_join ($join){
// current page, post type and $_GLOBAL['s'] checks omitted
$join .='LEFT JOIN '.$wpdb->postmeta. ' ON '. $wpdb->posts . '.ID = ' . $wpdb->postmeta . '.post_id ';
return $join;
}
function songs_search_where( $where ){
// current page, post type and $_GLOBAL['s'] checks omitted
$where = preg_replace(
"/\(\s*".$wpdb->posts.".post_title\s+LIKE\s*(\'[^\']+\')\s*\)/",
"(".$wpdb->posts.".post_title LIKE $1) OR (".$wpdb->postmeta.".meta_value LIKE $1)", $where
);
return $where;
}
カスタム フィールド値で用語を検索しようとすると、検索は正常に機能します。
ただし、これらの両方を順番に使用しようとすると(つまり、検索してからフィルター、またはその逆)、wp_postmetaを使用した検索結合と、それを使用した「カスタムフィールドチェックボックス値」によるフィルターで問題が発生します。これらの両方を順番に利用できるようにする方法はありますか?
受け取ったエラー: WordPress データベース エラー 一意のテーブル/エイリアスではありません: 'wp_postmeta'
結果のSQLクエリ出力:
SELECT SQL_CALC_FOUND_ROWS wp_posts.ID FROM wp_posts INNER JOIN wp_postmeta ON (wp_posts.ID = wp_postmeta.post_id)LEFT JOIN wp_postmeta ON wp_posts.ID = wp_postmeta.post_id WHERE 1=1 AND (((wp_posts.post_title LIKE '%searchterm%') OR (wp_postmeta.meta_value LIKE '%searchterm%') OR (wp_posts.post_content LIKE '%searchterm%'))) AND wp_posts.post_type = 'songs' AND (wp_posts.post_status = 'publish' OR wp_posts.post_status = 'future' OR wp_posts.post_status = 'draft' OR wp_posts.post_status = 'pending' OR wp_posts.post_status = 'private') AND ( (wp_postmeta.meta_key = 'sample_playlist' AND CAST(wp_postmeta.meta_value AS CHAR) = 'on') ) ORDER BY wp_posts.post_date DESC LIMIT 0, 20