スニペット/WordPress search
1 2 3 4 5 6 7 8 9 10 | // 検索結果を投稿記事のみに function search_only_post($query) { if ( is_admin() || ! $query->is_main_query() ) return; if ( $query->is_search() ) { $query->set('post_type', 'post'); return; } } add_action('pre_get_posts', 'search_only_post'); |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 | /** * Extend WordPress search to include custom fields * http://adambalee.com */ /** * Join posts and postmeta tables * http://codex.wordpress.org/Plugin_API/Filter_Reference/posts_join */ function cf_search_join($join) { global $wpdb; if (is_search()) { $join .=' LEFT JOIN '.$wpdb->postmeta. ' ON '. $wpdb->posts . '.ID = ' . $wpdb->postmeta . '.post_id '; } return $join; } add_filter('posts_join', 'cf_search_join'); /** * Modify the search query with posts_where * http://codex.wordpress.org/Plugin_API/Filter_Reference/posts_where */ function cf_search_where($where) { global $wpdb; if (is_search()) { $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; } add_filter('posts_where', 'cf_search_where'); /** * Prevent duplicates * http://codex.wordpress.org/Plugin_API/Filter_Reference/posts_distinct */ function cf_search_distinct($where) { global $wpdb; if (is_search()) { return "DISTINCT"; } return $where; } add_filter('posts_distinct', 'cf_search_distinct'); |
1 2 3 4 5 6 7 8 9 | <form action="<?php bloginfo('url'); ?>" method="get"> <label for="search"></label> <?php $args = array( 'show_option_all' => '全登録作品', 'child_of' => 10, // 92 'orderby' => 'name' ); ?> <?php wp_dropdown_categories($args); ?><input type="text" name="s" value="<?php the_search_query(); ?>" id="search" placeholder="検索"><input type="submit" value=""> </form> |
1 2 3 4 5 | <form action="/" method="get"> <label for="search"></label> <input type="text" name="s" value="<?php the_search_query(); ?>" id="search"><input type="submit" value="検索"> <input type="hidden" value="post" name="post_type" id="post_type"> </form> |
1 2 3 4 5 6 7 8 9 10 11 | <form action="<?php echo home_url('/'); ?>" method="get"> <label for="search"></label> <input type="submit" value=""> <input type="text" name="s" value="<?php the_search_query(); ?>" id="search"> <?php if (is_singular('before_after')): ?> <input type="hidden" value="before_after" name="post_type" id="post_type"> <?php else: ?> <input type="hidden" value="post" name="post_type" id="post_type"> <?php endif; ?> </form> |
1 2 3 4 5 6 7 8 9 10 11 12 | <?php if (have_posts()): ?> <ul id="block-archive"> <?php global $wp_query; $total_results = $wp_query -> found_posts; ?> <li><?php the_search_query(); ?></li> <!-- /#block-archive --></ul> <?php get_template_part('parts/loop'); ?> <?php get_template_part('parts/pager'); ?> <?php endif; ?> |
コメントを投稿する