ä¹ ãã¶ãã«Wordpressã触ã£ã¦ãã¾ããå¿ãã¦ããäºã ããã§å¤§å¤ã§ãï¼
ãã¦ãWordpressã®æ¤ç´¢ã§ããæã¯æç¨¿ã®ã¿ã対象ã ã£ãã¨è¨æ¶ãã¦ãã¾ãã
æ°ãããªã£ã¦åºå®ãã¼ã¸ãæ¤ç´¢çµæã«å«ã¾ããããã«ãªã£ãã£ã½ãã§ãã
åºå®ãã¼ã¸ãæ¤ç´¢çµæããé¤å¤ããã
ã§ãåºå®ãã¼ã¸ã¯æ¤ç´¢çµæããå¤ãããã¨æãã°ã°ãã¨
ä¸è¨ã®ãããªpre_get_posts
ã§queryãæ¸ãæããããæ¹ãå¤ãåºã¦ãã¾ããã
<?php /* functions.php */ function SearchFilter($query) { if ($query->is_search) { $query->set('post_type', 'post'); } return $query; } add_filter('pre_get_posts','SearchFilter');
ããããä¸è¨ã®æ¹æ³ã ã¨ã
ã©ã®ãã¼ã¯ã¼ãã§æ¤ç´¢ãã¦ãpost_type="post"
ã®è¨äºãå
¨ã¦è¡¨ç¤ºããã¦ãã¾ãã¾ããããã
ãªã®ã§ãæ¯è¼çæè¿æç¨¿ãããè¨äºãæ¢ãã¾ããã
(wordpressã®æ§ãªæ´æ°ãå¤ããã¤ãºãæ¤ç´¢ã«ããããããè¨äºã¯æç¨¿æ¥ã¯ããããããæè¨ãã¦æ¬²ããï¼åå®ã«!!)
ããã§posts_search
ãã£ã«ã¿ã¼ãä½¿ãæ¹æ³ãæ¸ããã¦ãã¾ããã®ã§ã
å
ã»ã©è¨è¿°ããSearchFilter
ãåé¤ãã¦ä¸è¨ãfunction.phpã«è¨è¿°ãã¾ããã
<?php /* functions.php */ function my_posy_search($search) { if(is_search()) { $search .= " AND post_type = 'post'"; } return $search; } add_filter('posts_search', 'my_posy_search');
ããã§ãæå¾
éãæ¤ç´¢çµæãpost_type="post"
ã®ãã®ã ãã«ãªãã¾ããï¼
ã«ã¹ã¿ã æç¨¿ã¿ã¤ããæ¤ç´¢çµæã«å«ããã
次ã«ãã«ã¹ã¿ã æç¨¿ã¿ã¤ãnews
ãæ¤ç´¢ã«å«ãããã¨æãã¾ããã
ã©ãããposts_search
ã®$searchã¯ã¯ã¨ãªã¼ã®WHEREæã«ãªããããªã®ã§ãããã§post_type = 'news'
ã追å ããã°ãã¾ãè¡ããããªæ°ããã¾ãã
<?php /* functions.php */ function my_posy_search($search) { if(is_search()) { // post_type='news'ãæ¤ç´¢çµæã«å«ãã $search .= " AND (post_type = 'post' OR post_type='news')"; } return $search; } add_filter('posts_search', 'my_posy_search');
ä¸è¨ã®æ§ã«WHEREæã«è¿½å ãã¾ããã
ããããæ¤ç´¢çµæã«ã¯post_type = 'post'
ã®ãã®ãã表示ããã¾ããã§ããããã
ä¸åº¦function.phpã«è¨è¿°ããçµãè¾¼ã¿ã®å¦çãã³ã¡ã³ãã¢ã¦ããã¦æ¤ç´¢çµæãè¦ã¦ã¿ãã¨ãããã©ãããå
ã
ã®ã¯ã¨ãªã«ã«ã¹ã¿ã æç¨¿ã¿ã¤ããå«ãã§ãªãã£ã½ãï¼ã¨ããäºãè§£ãã¾ããã
(ã«ã¹ã¿ã æç¨¿ã¿ã¤ãã¯æ¤ç´¢çµæã«åºã¦ããªãã£ã)
ãªã®ã§ãpre_get_posts
ã¢ã¯ã·ã§ã³ã§æ¤ç´¢æã«post_typeã追å ãã¦ã¿ã¾ããã
<?php /* function.php */ function my_posts_per_page($query) { if( is_search() ) { $query->set( 'post_type', array('post', 'news') ); } } add_action( 'pre_get_posts', 'my_posts_per_page' ); function my_posy_search($search) { if(is_search()) { // post_type='news'ãæ¤ç´¢çµæã«å«ãã $search .= " AND (post_type = 'post' OR post_type='news')"; } return $search; } add_filter('posts_search', 'my_posy_search');
ããã§ãåæ¤ç´¢ããçµæ...
æå¾
éã postã¨newsã®ä¸ããæ¤ç´¢çµæã表示ããã¾ããï¼
ãµã¤ãã«ã«ã¹ã¿ã æç¨¿ã®ãã¼ããªã³ã¯ã夿´ããWordPress › Custom Post Type Permalinks « WordPress Plugins ãã©ã°ã¤ã³ã使ç¨ãã¦ããäºãæç¨¿ãã¼ããªã³ã¯è¨å®ã夿´ãã¦ããäºãå½±é¿ããããããã¾ããããä¸è¨ã®æ¹æ³ã§æå¾ ããçµæãå®ç¾ãããã¨ãã§ãã¾ããã
調ã¹ã¦ããæãæ¤ç´¢ãã©ã¼ã ã«inputã¿ã°ãè¨è¿°ãã¦ãã¾ãæ¹æ³ãããããã§ãã
WordPress › Support » Limit Search Results to Custom Post Type?
<form method="get" action="<?php bloginfo( 'url' ); ?>"> <input type="hidden" name="post_type[]" value="post"> <input type="hidden" name="post_type[]" value="news"> <input name="s" id="s" type="text" /> <input id="submit" type="submit" value="æ¤ç´¢" /> </form>
ãã©ã¼ã©ã ãåèã«æ¤ç´¢ããã¯ã¹ã«input[type="hidden", name="post_type[]"]ã§æ¤ç´¢ã«å«ãããpost_typeãvalueã«ãã¦è¨è¿°ãã¦ããã°æ¤ç´¢çµæã«ã表示ããããã¨ãã§ãã¾ããã
ããæ¹ã¨ãã¦ã¯ãã¡ãã®æ¹ãç°¡åããããã¾ããã
ãã URLã«&post_type
ã¨åºã¦ãã¾ãã®ã§ããããå«ãªäººã¯function.phpã§ããã°ããã ããï¼
[åèã«ãã¾ãã]