{"id":1922,"date":"2011-02-01T17:13:15","date_gmt":"2011-01-31T23:13:15","guid":{"rendered":"http:\/\/www.moonmile.net\/blog\/archives\/1922"},"modified":"2014-06-22T16:50:54","modified_gmt":"2014-06-22T07:50:54","slug":"cakephp-%e3%81%8b%e3%82%89-wordpress-%e3%81%ae%e3%83%87%e3%83%bc%e3%82%bf%e3%82%92%e6%89%b1%e3%81%863","status":"publish","type":"post","link":"http:\/\/www.moonmile.net\/blog\/archives\/1922","title":{"rendered":"CakePHP \u304b\u3089 wordpress \u306e\u30c7\u30fc\u30bf\u3092\u6271\u3046(3)"},"content":{"rendered":"<p>\u4eca\u5ea6\u306f wordpress \u306e\u30ab\u30c6\u30b4\u30ea\u4e00\u89a7\u3001\u3068\u6307\u5b9a\u3057\u305f\u30ab\u30c6\u30b4\u30ea\u5185\u306e\u8a18\u4e8b\u3092\u62fe\u3063\u3066\u307f\u307e\u3059\u3002<\/p>\n<p>wordpress \u306f\u30ab\u30c6\u30b4\u30ea\u306e\u6271\u3044\u304c\u3084\u3084\u3053\u3057\u304f\u3066\u3001\u30ab\u30c6\u30b4\u30ea\u306e\u540d\u524d\u81ea\u4f53\u306f wp_terms \u306b\u5165\u3063\u3066\u3044\u308b\u306e\u3067\u3059\u304c&#8230;\u305d\u306e\u5f8c\u306e wp_posts \u30c6\u30fc\u30d6\u30eb\u307e\u3067\u306e\u95a2\u4fc2\u304c\u3084\u3084\u3053\u3057\u3044\u3068\u3044\u3046\u3002<br \/>\n# \u66f4\u306b\u4e91\u3048\u3070\u3001\u30ab\u30b9\u30bf\u30e0\u30d5\u30a3\u30fc\u30eb\u30c9\u3068\u95a2\u4fc2\u3092\u898b\u3066\u3044\u304f\u3068\u3001\u306a\u306b\u3084\u3089\u5927\u5909\u3068\u3044\u3046\u611f\u3058\u306a\u306e\u3067\u3059\u3002<\/p>\n<p><a href=\"http:\/\/www.moonmile.net\/blog\/wp-content\/uploads\/2011\/02\/wpid-dworkblogimage20110201_06org.jpg\"><img decoding=\"async\" src=\"http:\/\/www.moonmile.net\/blog\/wp-content\/uploads\/2011\/02\/wpid-dworkblogimage20110201_06thum.jpg\" border=\"0\" \/><\/a><\/p>\n<ul>\n<li>terms: \u30ab\u30c6\u30b4\u30ea\u540d\u306a\u3069\u306e\u540d\u79f0<\/li>\n<li>term_taxonomy: terms \u306e\u5206\u985e(category, link_category \u306a\u3069)<\/li>\n<li>term_relationships: \u30ab\u30c6\u30b4\u30ea\u3068\u306e\u89aa\u5b50\u95a2\u4fc2<\/li>\n<li>posts: \u8a18\u4e8b\u81ea\u4f53<\/li>\n<\/ul>\n<p>\u3068\u3044\u3046 4 \u3064\u306e\u30c6\u30fc\u30d6\u30eb\u304c\u95a2\u308f\u3063\u3066\u304d\u307e\u3059\u3002<\/p>\n<p>\u3044\u304d\u306a\u308a\u306f\u3084\u3084\u3053\u3057\u3044\u306e\u3067\u3001\u307e\u305a\u306f\u30ab\u30c6\u30b4\u30ea\u4e00\u89a7\u3002<\/p>\n<p>\u25a0\u30ab\u30c6\u30b4\u30ea\u4e00\u89a7\u306e\u30d3\u30e5\u30fc(views\/categories\/index.ctp)<\/p>\n<p>[code lang=&#8221;php&#8221;]<br \/>\n&lt;h2&gt;\u30ab\u30c6\u30b4\u30ea\u4e00\u89a7&lt;\/h2&gt;<br \/>\n&lt;table&gt;<br \/>\n\t&lt;tr&gt;<br \/>\n\t\t&lt;th&gt;term_id&lt;\/th&gt;<br \/>\n\t\t&lt;th&gt;term_taxonomy_id&lt;\/th&gt;<br \/>\n\t\t&lt;th&gt;name&lt;\/th&gt;<br \/>\n\t\t&lt;th&gt;slug&lt;\/th&gt;<br \/>\n\t&lt;\/tr&gt;<br \/>\n\t&lt;?php foreach($Categories as $item) : ?&gt;<br \/>\n\t&lt;tr&gt;<br \/>\n\t\t&lt;td&gt;&lt;?php echo $item[&#8216;Term&#8217;][&#8216;term_id&#8217;]; ?&gt;<br \/>\n\t\t&lt;td&gt;&lt;?php echo $item[&#8216;TermTaxonomy&#8217;][&#8216;term_taxonomy_id&#8217;]; ?&gt;<br \/>\n\t\t&lt;td&gt;&lt;?php echo $item[&#8216;Term&#8217;][&#8216;name&#8217;]; ?&gt;<br \/>\n\t\t&lt;td&gt;&lt;?php echo $item[&#8216;Term&#8217;][&#8216;slug&#8217;]; ?&gt;<br \/>\n\t&lt;\/tr&gt;<br \/>\n\t&lt;?php endforeach ; ?&gt;<br \/>\n&lt;\/table&gt;<br \/>\n&lt;h2&gt;\u30ab\u30c6\u30b4\u30ea\u4e00\u89a7(\u30ea\u30f3\u30af\u7248)&lt;\/h2&gt;<br \/>\n&lt;ul&gt;<br \/>\n\t&lt;?php foreach($Categories as $item) : ?&gt;<br \/>\n\t&lt;li&gt;<br \/>\n\t\t&lt;a href=&quot;http:\/\/localhost\/cake_wp\/categories\/item\/&lt;?php echo $item[&#8216;Term&#8217;][&#8216;slug&#8217;]; ?&gt;&quot;&gt;&lt;?php echo $item[&#8216;Term&#8217;][&#8216;name&#8217;]; ?&gt;&lt;\/a&gt;<br \/>\n\t&lt;\/li&gt;<br \/>\n\t&lt;?php endforeach ; ?&gt;<br \/>\n&lt;\/ul&gt;<br \/>\n[\/code]<\/p>\n<p>\u30ab\u30c6\u30b4\u30ea\u3060\u3051\u3092\u8868\u793a\u3057\u305f\u3044\u306e\u3067\u3001Term \u3068 TermTaxonomy \u30e2\u30c7\u30eb\u3060\u3051\u3092\u4f7f\u3044\u307e\u3059\u3002<\/p>\n<p>\u25a0\u30b3\u30f3\u30c8\u30ed\u30fc\u30e9\u30fc(controllers\/categories_controller.php)<\/p>\n<p>[code lang=&#8221;php&#8221;]<br \/>\n&lt;?php<br \/>\nclass CategoriesController extends AppController {<\/p>\n<p>\tvar $name = &#8216;Categories&#8217;;<br \/>\n\tvar $uses = array(&#8216;Category&#8217;);<br \/>\n\tfunction index() {<br \/>\n\t\t$this-&gt;set(&#8216;Categories&#8217;,$this-&gt;Category-&gt;findCategories());<br \/>\n\t}<br \/>\n\tfunction item($slug=&#8221;) {<br \/>\n\t\t$this-&gt;set(&#8216;Categories&#8217;,$this-&gt;Category-&gt;findPosts($slug));<br \/>\n\t\t$this-&gt;set(&#8216;ViewData&#8217;,array(<br \/>\n\t\t\t&#8216;slug&#8217;=&gt;$slug,<br \/>\n\t\t\t&#8216;name&#8217;=&gt; $this-&gt;Category-&gt;getCategoryName($slug)<br \/>\n\t\t\t\t));<br \/>\n\t}<br \/>\n}<br \/>\n?&gt;<br \/>\n[\/code]<\/p>\n<p>\uff08\u8ffd\u8a18 2014\/06\/22 item function \u3092\u8ffd\u52a0\uff09<br \/>\n\u30b3\u30f3\u30c8\u30ed\u30fc\u30e9\u30fc\u306f CategoriesController \u306b\u3057\u307e\u3057\u305f\u30024 \u3064\u306e\u30c6\u30fc\u30d6\u30eb\u3092\u4f7f\u3046\u306e\u3067\u3001\u3069\u308c\u306e\u540d\u524d\u3082\u9069\u5207\u3067\u306f\u306a\u3044\u3093\u3067\u3059\u3088\u306d\u3002\u305d\u3057\u3066\u3001\u4e00\u89a7\u8868\u3092\u53d6\u308b\u6642\u306b\u306f index \u30e1\u30bd\u30c3\u30c9\u3092\u4f5c\u3063\u3066\u3044\u307e\u3059\u3002<\/p>\n<p>\u3055\u3066\u3001\u30e2\u30c7\u30eb\u306f\u3069\u3046\u3059\u308b\u304b\u3068\u3044\u3046\u3068\u3001Term \u30e2\u30c7\u30eb\u3001TermTaxonomy \u30e2\u30c7\u30eb\u3092\u76f4\u63a5\u5229\u7528\u3059\u308b\u3053\u3068\u306f\u907f\u3051\u307e\u3057\u305f\u3002\u5148\u306b\u3082\u66f8\u304d\u307e\u3057\u305f\u304c\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u4e0a\u306f\u6b63\u898f\u5316\u3055\u308c\u3066\u3044\u308b\u3053\u3068\u304c\u5e38\u306a\u8a33\u3067\u3001\u305d\u306e\u307e\u307e O\/R \u30de\u30c3\u30d4\u30f3\u30b0\u3092\u3057\u3066\u3057\u307e\u3046\u3068\u3001\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u6307\u5411\u306e\u4e16\u754c\u3067\u306f\u300c\u73fe\u5b9f\u96e2\u308c\u300d\u306a\u30af\u30e9\u30b9\u306b\u306a\u3063\u3066\u3057\u307e\u3044\u307e\u3059\u3002\u306a\u306e\u3067\u3001\u3088\u308a\u81ea\u7136\u306a\u5f62\u306b\u623b\u3059(\u672c\u6765\u306e\u5f62\u306b\u623b\u3059)\u305f\u3081\u306b\u3001Category \u30e2\u30c7\u30eb\u30af\u30e9\u30b9\u3092\u4f5c\u308a\u307e\u3059\u3002<br \/>\n\u3053\u3053\u3067\u30ea\u30b9\u30c8\u3092\u53d6\u5f97\u3059\u308b\u305f\u3081\u306b\u3001findPosts \u3068\u3044\u3046\u30e1\u30bd\u30c3\u30c9\u3092\u65b0\u305f\u306b\u4f5c\u3063\u3066\u3044\u307e\u3059\u3002<br \/>\n# find(&#8216;all&#8217;) \u3068\u304b find(&#8216;first&#8217;) \u3068\u304b\u5408\u308f\u305b\u305f\u307b\u3046\u304c\u3088\u3044\u306e\u3067\u3059\u304c\u3001\u3072\u3068\u307e\u305a\u3002<\/p>\n<p>\u25a0\u30e2\u30c7\u30eb(models\/category.php)<\/p>\n<p>\u3055\u3066\u3001Category \u30e2\u30c7\u30eb\u306f\u5b9f\u30c6\u30fc\u30d6\u30eb\u3068\u306f\u9023\u7d50\u3057\u3066\u3044\u306a\u3044\u306e\u3067\u3001$useTable = false \u306b\u3057\u307e\u3059\u3002\u3067\u3001\u554f\u984c\u306a\u306e\u306f\u3001\u3069\u3046\u3084\u3063\u3066\u30c7\u30fc\u30bf\u3092\u53d6\u3063\u3066\u304f\u308b\u306e\u304b\uff1f\u3067\u3059\u306d\u3002<\/p>\n<p>\u5148\u306b Term \u3068 TermTaxonomy \u30e2\u30c7\u30eb\u306e\u30a2\u30bd\u30b7\u30a8\u30fc\u30b7\u30e7\u30f3\u3092\u8a2d\u5b9a\u3057\u307e\u3059\u3002<\/p>\n<p>[code lang=&#8221;php&#8221;]<br \/>\n&lt;!&#8211; models\/term.php &#8211;&gt;<br \/>\n&lt;?php<br \/>\nclass Term extends AppModel<br \/>\n{<br \/>\n\tvar $name = &#8216;Term&#8217;;<br \/>\n\tvar $primaryKey = &#8216;term_id&#8217;;<br \/>\n\tvar $hasOne = array(<br \/>\n\t\t&#8216;TermTaxonomy&#8217; =&gt; array(<br \/>\n\t\t\t&#8216;className&#8217; =&gt; &#8216;TermTaxonomy&#8217;,<br \/>\n\t\t\t&#8216;foreignKey&#8217; =&gt; &#8216;term_taxonomy_id&#8217; ));<br \/>\n}<br \/>\n?&gt;<br \/>\n[\/code]<\/p>\n<p>Term.term_taxonomy_id (1)-(1) TermTaxonomy.term_taxonomy_id \u306e\u95a2\u4fc2\u306b\u306a\u308b\u306e\u3067 Model::$hasOne \u3092\u6307\u5b9a\u3002<\/p>\n<p>[code lang=&#8221;php&#8221;]<br \/>\n&lt;!&#8211; models\/term_taxonomy.php &#8211;&gt;<br \/>\n&lt;?php<br \/>\nclass TermTaxonomy extends AppModel<br \/>\n{<br \/>\n\tvar $name = &#8216;TermTaxonomy&#8217;;<br \/>\n\tvar $useTable = &#8216;term_taxonomy&#8217;;<br \/>\n\tvar $primaryKey = &#8216;term_taxonomy_id&#8217;;<br \/>\n\tvar $hasOne = array(<br \/>\n\t    &#8216;Term&#8217; =&gt; array(<br \/>\n\t\t\t&#8216;className&#8217; =&gt; &#8216;Term&#8217;,<br \/>\n\t\t\t&#8216;foreignKey&#8217; =&gt; &#8216;term_id&#8217; ));<br \/>\n}<br \/>\n?&gt;<br \/>\n[\/code]<\/p>\n<p>\u9006\u5411\u304d\u3082\u540c\u3058\u3067 Model::$hasOne \u3092\u6307\u5b9a\u3002<\/p>\n<p>\u3053\u3046\u3057\u3066\u304a\u304f\u3068\u3001\u6b21\u306e\u3088\u3046\u306b findCategories \u304c\u66f8\u3051\u307e\u3059\u3002<\/p>\n<p>[code lang=&#8221;php&#8221;]<br \/>\n&lt;!&#8211; models\/category.php &#8211;&gt;<br \/>\n&lt;?php<br \/>\nclass Category extends AppModel<br \/>\n{<br \/>\n\tvar $name = &#8216;Category&#8217;;<br \/>\n\tvar $useTable = false ;<br \/>\n\tfunction findCategories() {<br \/>\n\t\tif (0) {<br \/>\n\t\t\t$TermTaxonomy = ClassRegistry::init(&#8216;TermTaxonomy&#8217;);<br \/>\n\t\t\t$items = $TermTaxonomy-&gt;find(&#8216;all&#8217;,array(<br \/>\n\t\t\t\t&#8216;conditions&#8217;=&gt;&quot;taxonomy = &#8216;category&#8217;&quot;));<br \/>\n\t\t\treturn $items ;<br \/>\n\t\t} else {<br \/>\n\t\t\t$Term = ClassRegistry::init(&#8216;Term&#8217;);<br \/>\n\t\t\t$items = $Term-&gt;find(&#8216;all&#8217;, array(<br \/>\n\t\t\t\t&#8216;conditions&#8217;=&gt;&quot;TermTaxonomy.taxonomy = &#8216;category&#8217;&quot;));<br \/>\n\t\t\treturn $items ;<br \/>\n\t\t}<br \/>\n\t}<br \/>\n}<br \/>\n[\/code]<\/p>\n<p>if \u6587\u3067\u5206\u3051\u3066\u3044\u307e\u3059\u304c\u3001\u3069\u3061\u3089\u3082\u540c\u3058\u8a18\u8ff0\u3067\u3059\u3002<br \/>\n\u30c6\u30fc\u30d6\u30eb\u306b\u9023\u7d50\u3057\u3066\u3044\u306a\u3044\u5834\u5408\u306f\u3001ClassRegistry::init \u30e1\u30bd\u30c3\u30c9\u3067\u30e2\u30c7\u30eb\u30af\u30e9\u30b9\u3092\u8aad\u307f\u8fbc\u307f\u307e\u3059\u3002\u3053\u306e\u5f8c\u306f\u3001\u666e\u901a\u306b\u691c\u7d22\u3092\u3057\u3066 OK \u3067\u3059\u3002<br \/>\nTermTaxonomy \u30e2\u30c7\u30eb\u3092\u4e2d\u5fc3\u306b\u3057\u305f\u5834\u5408\u306f\u3001&#8217;category&#8217; \u3092\u76f4\u63a5\u691c\u7d22\u3067\u304d\u307e\u3059\u304c\u3001Term \u30e2\u30c7\u30eb\u3092\u4e2d\u5fc3\u306b\u3057\u305f\u5834\u5408\u306f\u3001TermTaxonomy.taxonomy \u306a\u5f62\u3067 conditions \u3092\u8a2d\u5b9a\u3057\u307e\u3059\u3002<\/p>\n<p>\u3053\u308c\u306f\u3069\u3061\u3089\u3082\u7d50\u679c\u304c\u540c\u3058\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n<p>[img 20110201_07.jpg]<\/p>\n<p>\u4eca\u5ea6\u306f\u3001\u30ab\u30c6\u30b4\u30ea\u5185\u306e\u8a18\u4e8b\u4e00\u89a7\u3092\u53d6\u5f97\u3057\u307e\u3059\u3002<\/p>\n<p>\u25a0\u30ab\u30c6\u30b4\u30ea\u5185\u8a18\u4e8b\u306e\u30d3\u30e5\u30fc(views\/categories\/item.ctp)<\/p>\n<p>[code lang=&#8221;php&#8221;]<br \/>\n&lt;h2&gt;\u30ab\u30c6\u30b4\u30ea\u5185\u306e\u8a18\u4e8b\u4e00\u89a7&lt;\/h2&gt;<br \/>\n&lt;?php echo $ViewData[&#8216;slug&#8217;]; ?&gt; \/<br \/>\n&lt;?php echo $ViewData[&#8216;name&#8217;]; ?&gt;<br \/>\n&lt;table&gt;<br \/>\n\t&lt;tr&gt;<br \/>\n\t\t&lt;th&gt;Post.ID&lt;\/th&gt;<br \/>\n\t\t&lt;th&gt;Post.post_title&lt;\/th&gt;<br \/>\n\t\t&lt;th&gt;Post.post_date&lt;\/th&gt;<br \/>\n\t\t&lt;th&gt;Post.guid&lt;\/th&gt;<br \/>\n\t&lt;\/tr&gt;<br \/>\n\t&lt;?php foreach($Categories as $item) : ?&gt;<br \/>\n\t&lt;tr&gt;<br \/>\n\t\t&lt;td&gt;&lt;?php echo $item[&#8216;Post&#8217;][&#8216;ID&#8217;]; ?&gt;<br \/>\n\t\t&lt;td&gt;&lt;?php echo $item[&#8216;Post&#8217;][&#8216;post_title&#8217;]; ?&gt;<br \/>\n\t\t&lt;td&gt;&lt;?php echo $item[&#8216;Post&#8217;][&#8216;post_date&#8217;]; ?&gt;<br \/>\n\t\t&lt;td&gt;&lt;?php echo $item[&#8216;Post&#8217;][&#8216;guid&#8217;]; ?&gt;<br \/>\n\t&lt;\/tr&gt;<br \/>\n\t&lt;?php endforeach ; ?&gt;<br \/>\n&lt;\/table&gt;<br \/>\n[\/code]<\/p>\n<p>\u306a\u611f\u3058\u3067\u3001Post \u30e2\u30c7\u30eb\u304b\u3089\u60c5\u5831\u3092\u53d6\u3063\u3066\u304d\u307e\u3059\u3002\u30ab\u30c6\u30b4\u30ea\u306e\u30b9\u30e9\u30c3\u30b0(\u540d\u524d)\u3068\u65e5\u672c\u8a9e\u540d\u79f0\u306f\u3001$ViewData \u3068\u3044\u3046\u5909\u6570\u306b\u5165\u308c\u3066\u304a\u304d\u307e\u3059\u3002<\/p>\n<p>\u25a0\u307e\u305a\u306f\u3001\u30b3\u30f3\u30c8\u30ed\u30fc\u30e9\u30fc\u3092\u30af\u30a8\u30ea\u3067\u66f8\u3044\u3066\u3057\u307e\u3046\u3002<\/p>\n<p>\u9762\u5012\u306a\u306e\u3067\u3001Category::findPosts \u30e1\u30bd\u30c3\u30c9\u306e\u4e2d\u8eab\u3092\u30af\u30a8\u30ea\u3067\u66f8\u3044\u3066\u3057\u307e\u3063\u305f\u306e\u304c\u3053\u308c\u3067\u3059\u3002<\/p>\n<p>[code lang=&#8221;php&#8221;]<br \/>\n&lt;!&#8211; models\/category.php &#8211;&gt;<br \/>\n&lt;?php<br \/>\nclass Category extends AppModel<br \/>\n{<br \/>\n\tvar $name = &#8216;Category&#8217;;<br \/>\n\tvar $useTable = false ;<br \/>\n\tfunction findCategories() {<br \/>\n\t\tif (0) {<br \/>\n\t\t$TermTaxonomy = ClassRegistry::init(&#8216;TermTaxonomy&#8217;);<br \/>\n\t\t$items = $TermTaxonomy-&gt;find(&#8216;all&#8217;,array(<br \/>\n\t\t\t&#8216;conditions&#8217;=&gt;&quot;taxonomy = &#8216;category&#8217;&quot;));<br \/>\n\t\treturn $items ;<br \/>\n\t\t} else {<br \/>\n\t\t$Term = ClassRegistry::init(&#8216;Term&#8217;);<br \/>\n\t\t$items = $Term-&gt;find(&#8216;all&#8217;, array(<br \/>\n\t\t\t&#8216;conditions&#8217;=&gt;&quot;TermTaxonomy.taxonomy = &#8216;category&#8217;&quot;));<br \/>\n\t\treturn $items ;<br \/>\n\t\t}<br \/>\n\t}<\/p>\n<p>\t\/\/ \u30ab\u30c6\u30b4\u30ea\u5185\u306e\u8a18\u4e8b\u4e00\u89a7\u3092\u53d6\u5f97\u3059\u308b<br \/>\n\tfunction findPosts($slug,$max=10) {<br \/>\n\tif(0) {<\/p>\n<p>\t\t$sql = &lt;&lt;&lt; HERE<br \/>\nselect *<br \/>\nfrom wp_term_taxonomy as TermTaxonomy<br \/>\n inner join wp_term_relationships as TermRelationship<br \/>\n on TermTaxonomy.term_taxonomy_id = TermRelationship.term_taxonomy_id<br \/>\n inner join wp_terms as Term<br \/>\n  on ( Term.term_id = TermTaxonomy.term_id )<br \/>\n inner join wp_posts as Post<br \/>\n  on ( TermRelationship.object_id = Post.ID )<br \/>\n  where TermTaxonomy.taxonomy = &#8216;category&#8217;<br \/>\n   and Term.slug = &#8216;$slug&#8217;<br \/>\n order by Post.post_date desc<br \/>\n limit $max<br \/>\n ;<br \/>\nHERE;<br \/>\n\treturn $this-&gt;query($sql);<br \/>\n\t} else {<br \/>\n\t\t$Post = ClassRegistry::init(&#8216;Post&#8217;);<br \/>\n\t\t$items = $Post-&gt;find(&#8216;all&#8217;,array(<br \/>\n\t\t\t&#8216;conditions&#8217;=&gt;&quot;TermTaxonomy.taxonomy = &#8216;category&#8217;&quot;));<br \/>\n\t\treturn $items ;<br \/>\n\t}<\/p>\n<p>\t}<\/p>\n<p>\tfunction getCategoryName($slug) {<br \/>\n\t\t$Term = ClassRegistry::init(&#8216;Term&#8217;);<br \/>\n\t\t$item = $Term-&gt;find(&#8216;first&#8217;,array(<br \/>\n\t\t\t&#8216;conditions&#8217;=&gt;&quot;slug = &#8216;$slug&#8217;&quot;));<br \/>\n\t\treturn $item[&#8216;Term&#8217;][&#8216;name&#8217;];<br \/>\n\t}<br \/>\n}<br \/>\n?&gt;<br \/>\n[\/code]<\/p>\n<p>\u3053\u308c\u3067\u3082\u7528\u306f\u8db3\u308a\u308b\u306e\u3067\u3059\u304c\u3001\u30d7\u30ec\u30d5\u30a3\u30af\u30b9\u300cwp_\u300d\u304c\u3001\u305d\u306e\u307e\u307e\u51fa\u3066\u304d\u3066\u3057\u307e\u3046\u306e\u3067\u96e3\u70b9\u304c\u591a\u3044\u3067\u3059\u3088\u306d\u3002\u305f\u3060\u3001\u8907\u96d1\u306a\u30a2\u30bd\u30b7\u30a8\u30fc\u30b7\u30e7\u30f3\uff08\u5916\u90e8\u30ad\u30fc\uff09\u3067\u60a9\u3080\u3088\u308a\u306f\u3001\u3053\u306e\u307b\u3046\u304c\u5b89\u5168\u3060\u3068\u601d\u3044\u307e\u3059\u3002<\/p>\n<p><a href=\"http:\/\/www.moonmile.net\/blog\/wp-content\/uploads\/2011\/02\/wpid-dworkblogimage20110201_08org3.jpg\"><img decoding=\"async\" src=\"http:\/\/www.moonmile.net\/blog\/wp-content\/uploads\/2011\/02\/wpid-dworkblogimage20110201_08thum3.jpg\" border=\"0\" \/><\/a><\/p>\n<p>\u25a0\u30a2\u30bd\u30b7\u30a8\u30fc\u30b7\u30e7\u30f3\u3067\u8a2d\u5b9a\u3057\u3066\u307f\u308b\u3002<\/p>\n<p>\u304c\u3001CakePHP \u306a\u306e\u3067\u30a2\u30bd\u30b7\u30a8\u30fc\u30b7\u30e7\u30f3\u3067\u9811\u5f35\u3063\u3066\u307f\u3088\u3046\u3068\u30e2\u30c7\u30eb\u3092\u4f5c\u308a\u307e\u3059\u3002\u7d50\u8ad6\u304b\u3089\u8a00\u3046\u3068\u30014 \u3064\u306e\u30c6\u30fc\u30d6\u30eb\u3092\u9023\u643a\u3055\u305b\u3066\u52d5\u304b\u3059\u3053\u3068\u304c\u3067\u304d\u307e\u305b\u3093&#8230;\u679c\u305f\u3057\u3066\u52d5\u304f\u306e\u304b\u52d5\u304b\u306a\u3044\u306e\u304b\u5206\u304b\u3089\u306a\u3044\u306e\u3067\u3059\u304c\u3001\u4e00\u5fdc\u3001\u30bd\u30fc\u30b9\u3092\u6652\u3057\u3066\u304a\u304d\u307e\u3059\u3002<\/p>\n<p>TermRelationship \u30e2\u30c7\u30eb\u30af\u30e9\u30b9\u306f\u30d7\u30e9\u30a4\u30de\u30ea\u30fc\u30ad\u30fc\u30922\u3064\u6301\u3064\u306e\u3067\u3001$primaryKey \u306b\u5bfe\u3057\u3066 array \u3067\u6307\u5b9a\u3057\u3066\u3044\u308b\u306e\u3067\u3059\u304c\u3001\u3053\u308c\u3067\u3044\u3044\u3093\u3067\u3057\u3087\u3046\u304b\u306d\u3047\uff1f<\/p>\n<p>[code lang=&#8221;php&#8221;]<br \/>\n&lt;!&#8211; models\/term_relationship.php &#8211;&gt;<br \/>\n&lt;?php<br \/>\nclass TermRelationship extends AppModel<br \/>\n{<br \/>\n\tvar $name = &#8216;TermRelationship&#8217;;<br \/>\n\tvar $primaryKey = array(&#8216;object_id&#8217;,&#8217;term_taxonomy_id&#8217;);<br \/>\n\tvar $belongsTo = array(<br \/>\n\t\t&#8216;TermTaxonomy&#8217; =&gt; array(<br \/>\n\t\t\t&#8216;className&#8217; =&gt; &#8216;TermTaxonomy&#8217;,<br \/>\n\t\t\t&#8216;conditions&#8217; =&gt; &#8216;TermTaxonomy.term_taxonomy_id = TermRelationship.term_taxonomy_id&#8217;));<br \/>\n\tvar $hasOne = array(<br \/>\n\t\t&#8216;Post&#8217; =&gt; array(<br \/>\n\t\t\t&#8216;className&#8217; =&gt; &#8216;Post&#8217;,<br \/>\n\t\t\t&#8216;conditions&#8217; =&gt; &#8216;Post.ID = TermRelationship.object_id&#8217;));<br \/>\n}<br \/>\n?&gt;<br \/>\n[\/code]<\/p>\n<p>Post \u30e2\u30c7\u30eb\u306b\u5bfe\u3057\u3066\u306f\u3001Model::$hasOne \u3092\u4f7f\u3044\u3001TermTaxonomy \u30e2\u30c7\u30eb\u306b\u5bfe\u3057\u3066\u306f Model::$belongsTo \u306a\u3093\u3067\u3059\u304c\u3002<br \/>\nPost \u30e2\u30c7\u30eb\u306b\u95a2\u3057\u3066\u306f\u3001TermRelationship \u3078\u306e\u9023\u643a\u3092 Model::$hasOne \u3067\u6307\u5b9a\u3057\u3066\u304a\u304d\u307e\u3059\u3002<\/p>\n<p>[code lang=&#8221;php&#8221;]<br \/>\n&lt;!&#8211; models\/post.php &#8211;&gt;<br \/>\n&lt;?php<br \/>\nclass Post extends AppModel<br \/>\n{<br \/>\n\tvar $name = &#8216;Post&#8217;;<\/p>\n<p>\tvar $belongsTo = array(<br \/>\n\t    &#8216;User&#8217; =&gt; array(<br \/>\n\t\t\t&#8216;className&#8217; =&gt; &#8216;User&#8217;,<br \/>\n\t\t\t&#8216;foreignKey&#8217; =&gt; &#8216;post_author&#8217; ));<br \/>\n\tvar $hasOne = array(<br \/>\n\t\t&#8216;TermRelationship&#8217; =&gt; array(<br \/>\n\t\t\t&#8216;className&#8217; =&gt; &#8216;TermRelationship&#8217;,<br \/>\n\t\t\t&#8216;foreignKey&#8217; =&gt; &#8216;object_id&#8217;));<br \/>\n}<br \/>\n[\/code]<\/p>\n<p>\u3053\u308c\u3092\u6709\u52b9\u306b\u3057\u3066\u3001Category::findPosts \u30e1\u30bd\u30c3\u30c9\u3092\u66f8\u304d\u63db\u3048\u307e\u3059\u3002<\/p>\n<p>[code lang=&#8221;php&#8221;]<br \/>\n\t\/\/ \u30ab\u30c6\u30b4\u30ea\u5185\u306e\u8a18\u4e8b\u4e00\u89a7\u3092\u53d6\u5f97\u3059\u308b<br \/>\n\tfunction findPosts($slug,$max=10) {<br \/>\n\t\t$TermTaxonomy = ClassRegistry::init(&#8216;TermTaxonomy&#8217;);<br \/>\n\t\t$items = $TermTaxonomy-&gt;find(&#8216;all&#8217;,array(<br \/>\n\t\t\t&#8216;conditions&#8217;=&gt; array(<br \/>\n\t\t\t\t&#8216;TermTaxonomy.taxonomy&#8217; =&gt; &#8216;category&#8217;,<br \/>\n\t\t\t\t&#8216;Post.post_status&#8217; =&gt; &#8216;publish&#8217; )));<br \/>\n\t\treturn $items ;<br \/>\n\t}<br \/>\n[\/code]<\/p>\n<p>\u3053\u308c\u3092\u52d5\u304b\u3057\u3066\u307f\u308b\u306e\u3067\u3059\u304c\u3001SQL\u6587\u3067\u30a8\u30e9\u30fc\u304c\u51fa\u307e\u3059\u3002<\/p>\n<p>[code lang=&#8221;sql&#8221;]<br \/>\nSELECT<br \/>\n `TermTaxonomy`.`term_taxonomy_id`,<br \/>\n `TermTaxonomy`.`term_id`,<br \/>\n `TermTaxonomy`.`taxonomy`,<br \/>\n `TermTaxonomy`.`description`,<br \/>\n `TermTaxonomy`.`parent`,<br \/>\n `TermTaxonomy`.`count`,<br \/>\n `Post`.`ID`,<br \/>\n `Post`.`post_author`,<br \/>\n `Post`.`post_date`,<br \/>\n `Post`.`post_date_gmt`,<br \/>\n `Post`.`post_content`,<br \/>\n `Post`.`post_title`,<br \/>\n `Post`.`post_excerpt`,<br \/>\n `Post`.`post_status`,<br \/>\n `Post`.`comment_status`,<br \/>\n `Post`.`ping_status`,<br \/>\n `Post`.`post_password`,<br \/>\n `Post`.`post_name`,<br \/>\n `Post`.`to_ping`,<br \/>\n `Post`.`pinged`,<br \/>\n `Post`.`post_modified`,<br \/>\n `Post`.`post_modified_gmt`,<br \/>\n `Post`.`post_content_filtered`,<br \/>\n `Post`.`post_parent`,<br \/>\n `Post`.`guid`,<br \/>\n `Post`.`menu_order`,<br \/>\n `Post`.`post_type`,<br \/>\n `Post`.`post_mime_type`,<br \/>\n `Post`.`comment_count`,<br \/>\n `Term`.`term_id`,<br \/>\n `Term`.`name`,<br \/>\n `Term`.`slug`,<br \/>\n `Term`.`term_group`<br \/>\nFROM<br \/>\n `wp_term_taxonomy` AS `TermTaxonomy`<br \/>\n   LEFT JOIN `wp_posts` AS `Post`<br \/>\n    ON (`TermRelationship`.`object_id` = `Post`.`ID` AND `TermTaxonomy`.`post_id` = `Post`.`id`)<br \/>\n   LEFT JOIN `wp_terms` AS `Term`<br \/>\n    ON (`Term`.`term_id` = `TermTaxonomy`.`term_taxonomy_id`)<br \/>\nWHERE<br \/>\n `TermTaxonomy`.`taxonomy` = &#8216;category&#8217;<br \/>\nAND `Post`.`post_status` = &#8216;publish&#8217;<br \/>\n[\/code]<\/p>\n<p>`TermRelationship`.`object_id` \u304c\u7121\u3044\u3068\u306e\u3053\u3068\u306a\u306e\u3067\u3001TermRelationship \u306e\u5b9a\u7fa9\u304c\u60aa\u3044\u306e\u3067\u3059\u304c\u3001\u3061\u3087\u3063\u3068\u3088\u304f\u308f\u304b\u308a\u307e\u305b\u3093\u3002TermRelationship \u30e2\u30c7\u30eb\u81ea\u4f53\u306f\u3001\u30d7\u30e9\u30a4\u30de\u30ea\u30ad\u30fc\u304c object_id \u3068 term_taxonomy_id \u306e 2 \u3064\u306b\u306a\u308b\u306e\u3067\u3001\u3053\u308c\u304c\u539f\u56e0\u3067\u3057\u3087\u3046\u3002<\/p>\n<p>\u5f8c\u3067\u3082\u3046\u5c11\u3057\u8abf\u3079\u3066\u307f\u307e\u3059\u304c\u3001\u3053\u306e\u624b\u306e\u8907\u96d1\u306a\u30c6\u30fc\u30d6\u30eb\u306e\u5834\u5408\u306f\u30af\u30a8\u30ea\u6587\u304c\u697d\u304b\u306a\u3068\u601d\u3063\u3066\u3057\u307e\u3044\u307e\u3059\u3002\u307e\u3042\u3001\u4f5c\u696d\u91cf\u3092\u8003\u3048\u308b\u3068\u3001CakePHP \u306b\u6e96\u62e0\u3057\u3066\u306a\u3044\u30c6\u30fc\u30d6\u30eb\u3092\u6271\u3046\u6642\u306f(wordpress \u306f\u3001\u6e96\u62e0\u3057\u3066\u3044\u308b\u3068\u601d\u3046\u306e\u3060\u3051\u3069)\u30af\u30a8\u30ea\u306e\u307b\u3046\u304c\u4f5c\u6210\u304c\u901f\u3044\u3063\u3066\u306e\u304c\u3042\u308a\u307e\u3059\u306d\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4eca\u5ea6\u306f wordpress \u306e\u30ab\u30c6\u30b4\u30ea\u4e00\u89a7\u3001\u3068\u6307\u5b9a\u3057\u305f\u30ab\u30c6\u30b4\u30ea\u5185\u306e\u8a18\u4e8b\u3092\u62fe\u3063\u3066\u307f\u307e\u3059\u3002 wordpress \u306f\u30ab\u30c6\u30b4\u30ea\u306e\u6271\u3044\u304c\u3084\u3084\u3053\u3057\u304f\u3066\u3001\u30ab\u30c6\u30b4\u30ea\u306e\u540d\u524d\u81ea\u4f53\u306f wp_terms \u306b\u5165\u3063\u3066\u3044\u308b\u306e\u3067\u3059\u304c&#8230;\u305d\u306e\u5f8c &hellip; <a href=\"http:\/\/www.moonmile.net\/blog\/archives\/1922\">\u7d9a\u304d\u3092\u8aad\u3080 <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_feature_clip_id":0,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_post_was_ever_published":false},"categories":[20,13],"tags":[],"class_list":["post-1922","post","type-post","status-publish","format-standard","hentry","category-cakephp","category-wordpress"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"http:\/\/www.moonmile.net\/blog\/wp-json\/wp\/v2\/posts\/1922","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.moonmile.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.moonmile.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.moonmile.net\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/www.moonmile.net\/blog\/wp-json\/wp\/v2\/comments?post=1922"}],"version-history":[{"count":3,"href":"http:\/\/www.moonmile.net\/blog\/wp-json\/wp\/v2\/posts\/1922\/revisions"}],"predecessor-version":[{"id":5947,"href":"http:\/\/www.moonmile.net\/blog\/wp-json\/wp\/v2\/posts\/1922\/revisions\/5947"}],"wp:attachment":[{"href":"http:\/\/www.moonmile.net\/blog\/wp-json\/wp\/v2\/media?parent=1922"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.moonmile.net\/blog\/wp-json\/wp\/v2\/categories?post=1922"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.moonmile.net\/blog\/wp-json\/wp\/v2\/tags?post=1922"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}