我意识到改进WordPress的搜索功能需要大量蠕虫,但我需要它做的唯一额外的事情是在搜索作者姓名时显示结果。
例如,如果我们有一位名叫凯蒂·约翰逊的作者,我搜索凯蒂,我会在内容中列出她的名字的地方得到结果,但不会得到她发布的结果。
这似乎是一个简单的问题,但我还没有找到解决方案。
谢谢
我意识到改进WordPress的搜索功能需要大量蠕虫,但我需要它做的唯一额外的事情是在搜索作者姓名时显示结果。
例如,如果我们有一位名叫凯蒂·约翰逊的作者,我搜索凯蒂,我会在内容中列出她的名字的地方得到结果,但不会得到她发布的结果。
这似乎是一个简单的问题,但我还没有找到解决方案。
谢谢
也许您可以尝试直接在查询字符串中添加您的条件,使用类似的方法
function wpse_29570_where_filter($where){
global $wpdb;
if( is_search() ) {
$search= get_query_var(\'s\');
$query=$wpdb->prepare("SELECT user_id FROM $wpdb->usermeta WHERE ( meta_key=\'first_name\' AND meta_value LIKE \'%%%s%%\' ) or ( meta_key=\'last_name\' AND meta_value LIKE \'%%%s%%\' )", $search ,$search);
$authorID= $wpdb->get_var( $query );
if($authorID){
$where = " AND ( wp_posts.post_author = {$authorID} ) ";
}
}
return $where;
}
add_filter(\'posts_where\',\'wpse_29570_where_filter\');
改进了@hacksy的答案,我使用了相同的代码块,但将其更改为使用WP Job Manager plugin.
function add_author_to_search($where) {
global $wpdb;
if( is_search() ) { // Check if a search is made
$search = get_query_var(\'s\');
$like = \'%\' . $wpdb->esc_like( $search ) . \'%\';
$query = $wpdb->prepare( "SELECT user_id FROM $wpdb->usermeta WHERE ( meta_key=\'first_name\' AND lower(meta_value) LIKE \'%%%s%%\' ) OR ( meta_key=\'last_name\' AND lower(meta_value) LIKE \'%%%s%%\' ) OR ( meta_key=\'_company_name\' AND lower(meta_value) LIKE \'%%%s%%\' )", $search, $search, $search);
$authorID = $wpdb->get_var( $query );
if ($authorID) { // If a user_id matches a user
$where .= " OR " .$wpdb->posts. ".post_author = {$authorID} AND " .$wpdb->posts. ".post_name LIKE \'" .$like. "\'"; // Add to query of function: $GLOBALS[$WP_QUERY_VAR]->request
}
}
return $where;
}
add_filter(\'posts_where\',\'add_author_to_search\');
它所做的是搜索meta_key
的列first_name
, last_name
, 和_company_name
值,然后检查meta_value
查看它是否与搜索查询匹配。如果找到用户id,则会附加SQL search查询以搜索post\\u作者id,如果在post_name
柱我无法找到对我有用的答案,但我发现:https://danielbachhuber.com/2012/02/07/include-posts-by-matching-authors-in-your-search-results/
在未进行任何编辑的情况下工作。以下是链接过期时的代码:
/**
* Include posts from authors in the search results where
* either their display name or user login matches the query string
*
* @author danielbachhuber
*/
add_filter( \'posts_search\', \'db_filter_authors_search\' );
function db_filter_authors_search( $posts_search ) {
// Don\'t modify the query at all if we\'re not on the search template
// or if the LIKE is empty
if ( !is_search() || empty( $posts_search ) )
return $posts_search;
global $wpdb;
// Get all of the users of the blog and see if the search query matches either
// the display name or the user login
add_filter( \'pre_user_query\', \'db_filter_user_query\' );
$search = sanitize_text_field( get_query_var( \'s\' ) );
$args = array(
\'count_total\' => false,
\'search\' => sprintf( \'*%s*\', $search ),
\'search_fields\' => array(
\'display_name\',
\'user_login\',
),
\'fields\' => \'ID\',
);
$matching_users = get_users( $args );
remove_filter( \'pre_user_query\', \'db_filter_user_query\' );
// Don\'t modify the query if there aren\'t any matching users
if ( empty( $matching_users ) )
return $posts_search;
// Take a slightly different approach than core where we want all of the posts from these authors
$posts_search = str_replace( \')))\', ")) OR ( {$wpdb->posts}.post_author IN (" . implode( \',\', array_map( \'absint\', $matching_users ) ) . ")))", $posts_search );
return $posts_search;
}
/**
* Modify get_users() to search display_name instead of user_nicename
*/
function db_filter_user_query( &$user_query ) {
if ( is_object( $user_query ) )
$user_query->query_where = str_replace( "user_nicename LIKE", "display_name LIKE", $user_query->query_where );
return $user_query;
}
我正在使用gpp幻灯片,它会覆盖wordpress的默认库显示。我只想在我的自定义帖子类型“listings”上使用它-我如何引用它来仅替换wordpress在“listings”页面上的默认库?我的函数中有此代码。php,我似乎无法获得自定义帖子类型的正确参考:add_action(\'wp_head\',\'add_gpp_gallery\'); function add_gpp_gallery() { if( ( is_single() || is_page() ) &