{"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<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\n&lt;h2&gt;\u30ab\u30c6\u30b4\u30ea\u4e00\u89a7&lt;\/h2&gt;\r\n&lt;table&gt;\r\n\t&lt;tr&gt;\r\n\t\t&lt;th&gt;term_id&lt;\/th&gt;\r\n\t\t&lt;th&gt;term_taxonomy_id&lt;\/th&gt;\r\n\t\t&lt;th&gt;name&lt;\/th&gt;\r\n\t\t&lt;th&gt;slug&lt;\/th&gt;\r\n\t&lt;\/tr&gt;\r\n\t&lt;?php foreach($Categories as $item) : ?&gt;\r\n\t&lt;tr&gt;\r\n\t\t&lt;td&gt;&lt;?php echo $item&#x5B;&#039;Term&#039;]&#x5B;&#039;term_id&#039;]; ?&gt;\r\n\t\t&lt;td&gt;&lt;?php echo $item&#x5B;&#039;TermTaxonomy&#039;]&#x5B;&#039;term_taxonomy_id&#039;]; ?&gt;\r\n\t\t&lt;td&gt;&lt;?php echo $item&#x5B;&#039;Term&#039;]&#x5B;&#039;name&#039;]; ?&gt;\r\n\t\t&lt;td&gt;&lt;?php echo $item&#x5B;&#039;Term&#039;]&#x5B;&#039;slug&#039;]; ?&gt;\r\n\t&lt;\/tr&gt;\r\n\t&lt;?php endforeach ; ?&gt;\r\n&lt;\/table&gt;\r\n&lt;h2&gt;\u30ab\u30c6\u30b4\u30ea\u4e00\u89a7(\u30ea\u30f3\u30af\u7248)&lt;\/h2&gt;\r\n&lt;ul&gt;\r\n\t&lt;?php foreach($Categories as $item) : ?&gt;\r\n\t&lt;li&gt;\r\n\t\t&lt;a href=&amp;quot;http:\/\/localhost\/cake_wp\/categories\/item\/&lt;?php echo $item&#x5B;&#039;Term&#039;]&#x5B;&#039;slug&#039;]; ?&gt;&amp;quot;&gt;&lt;?php echo $item&#x5B;&#039;Term&#039;]&#x5B;&#039;name&#039;]; ?&gt;&lt;\/a&gt;\r\n\t&lt;\/li&gt;\r\n\t&lt;?php endforeach ; ?&gt;\r\n&lt;\/ul&gt;\r\n<\/pre>\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<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\n&lt;?php\r\nclass CategoriesController extends AppController {\r\n\r\n\tvar $name = &#039;Categories&#039;;\r\n\tvar $uses = array(&#039;Category&#039;);\r\n\tfunction index() {\r\n\t\t$this-&gt;set(&#039;Categories&#039;,$this-&gt;Category-&gt;findCategories());\r\n\t}\r\n\tfunction item($slug=&#039;&#039;) {\r\n\t\t$this-&gt;set(&#039;Categories&#039;,$this-&gt;Category-&gt;findPosts($slug));\r\n\t\t$this-&gt;set(&#039;ViewData&#039;,array(\r\n\t\t\t&#039;slug&#039;=&gt;$slug,\r\n\t\t\t&#039;name&#039;=&gt; $this-&gt;Category-&gt;getCategoryName($slug)\r\n\t\t\t\t));\r\n\t}\r\n}\r\n?&gt;\r\n<\/pre>\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<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\n&lt;!-- models\/term.php --&gt;\r\n&lt;?php\r\nclass Term extends AppModel\r\n{\r\n\tvar $name = &#039;Term&#039;;\r\n\tvar $primaryKey = &#039;term_id&#039;;\r\n\tvar $hasOne = array(\r\n\t\t&#039;TermTaxonomy&#039; =&gt; array(\r\n\t\t\t&#039;className&#039; =&gt; &#039;TermTaxonomy&#039;,\r\n\t\t\t&#039;foreignKey&#039; =&gt; &#039;term_taxonomy_id&#039; ));\r\n}\r\n?&gt;\r\n<\/pre>\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<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\n&lt;!-- models\/term_taxonomy.php --&gt;\r\n&lt;?php\r\nclass TermTaxonomy extends AppModel\r\n{\r\n\tvar $name = &#039;TermTaxonomy&#039;;\r\n\tvar $useTable = &#039;term_taxonomy&#039;;\r\n\tvar $primaryKey = &#039;term_taxonomy_id&#039;;\r\n\tvar $hasOne = array(\r\n\t    &#039;Term&#039; =&gt; array(\r\n\t\t\t&#039;className&#039; =&gt; &#039;Term&#039;,\r\n\t\t\t&#039;foreignKey&#039; =&gt; &#039;term_id&#039; ));\r\n}\r\n?&gt;\r\n<\/pre>\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<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\n&lt;!-- models\/category.php --&gt;\r\n&lt;?php\r\nclass Category extends AppModel\r\n{\r\n\tvar $name = &#039;Category&#039;;\r\n\tvar $useTable = false ;\r\n\tfunction findCategories() {\r\n\t\tif (0) {\r\n\t\t\t$TermTaxonomy = ClassRegistry::init(&#039;TermTaxonomy&#039;);\r\n\t\t\t$items = $TermTaxonomy-&gt;find(&#039;all&#039;,array(\r\n\t\t\t\t&#039;conditions&#039;=&gt;&amp;quot;taxonomy = &#039;category&#039;&amp;quot;));\r\n\t\t\treturn $items ;\r\n\t\t} else {\r\n\t\t\t$Term = ClassRegistry::init(&#039;Term&#039;);\r\n\t\t\t$items = $Term-&gt;find(&#039;all&#039;, array(\r\n\t\t\t\t&#039;conditions&#039;=&gt;&amp;quot;TermTaxonomy.taxonomy = &#039;category&#039;&amp;quot;));\r\n\t\t\treturn $items ;\r\n\t\t}\r\n\t}\r\n}\r\n<\/pre>\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<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\n&lt;h2&gt;\u30ab\u30c6\u30b4\u30ea\u5185\u306e\u8a18\u4e8b\u4e00\u89a7&lt;\/h2&gt;\r\n&lt;?php echo $ViewData&#x5B;&#039;slug&#039;]; ?&gt; \/\r\n&lt;?php echo $ViewData&#x5B;&#039;name&#039;]; ?&gt;\r\n&lt;table&gt;\r\n\t&lt;tr&gt;\r\n\t\t&lt;th&gt;Post.ID&lt;\/th&gt;\r\n\t\t&lt;th&gt;Post.post_title&lt;\/th&gt;\r\n\t\t&lt;th&gt;Post.post_date&lt;\/th&gt;\r\n\t\t&lt;th&gt;Post.guid&lt;\/th&gt;\r\n\t&lt;\/tr&gt;\r\n\t&lt;?php foreach($Categories as $item) : ?&gt;\r\n\t&lt;tr&gt;\r\n\t\t&lt;td&gt;&lt;?php echo $item&#x5B;&#039;Post&#039;]&#x5B;&#039;ID&#039;]; ?&gt;\r\n\t\t&lt;td&gt;&lt;?php echo $item&#x5B;&#039;Post&#039;]&#x5B;&#039;post_title&#039;]; ?&gt;\r\n\t\t&lt;td&gt;&lt;?php echo $item&#x5B;&#039;Post&#039;]&#x5B;&#039;post_date&#039;]; ?&gt;\r\n\t\t&lt;td&gt;&lt;?php echo $item&#x5B;&#039;Post&#039;]&#x5B;&#039;guid&#039;]; ?&gt;\r\n\t&lt;\/tr&gt;\r\n\t&lt;?php endforeach ; ?&gt;\r\n&lt;\/table&gt;\r\n<\/pre>\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<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\n&lt;!-- models\/category.php --&gt;\r\n&lt;?php\r\nclass Category extends AppModel\r\n{\r\n\tvar $name = &#039;Category&#039;;\r\n\tvar $useTable = false ;\r\n\tfunction findCategories() {\r\n\t\tif (0) {\r\n\t\t$TermTaxonomy = ClassRegistry::init(&#039;TermTaxonomy&#039;);\r\n\t\t$items = $TermTaxonomy-&gt;find(&#039;all&#039;,array(\r\n\t\t\t&#039;conditions&#039;=&gt;&amp;quot;taxonomy = &#039;category&#039;&amp;quot;));\r\n\t\treturn $items ;\r\n\t\t} else {\r\n\t\t$Term = ClassRegistry::init(&#039;Term&#039;);\r\n\t\t$items = $Term-&gt;find(&#039;all&#039;, array(\r\n\t\t\t&#039;conditions&#039;=&gt;&amp;quot;TermTaxonomy.taxonomy = &#039;category&#039;&amp;quot;));\r\n\t\treturn $items ;\r\n\t\t}\r\n\t}\r\n\r\n\t\/\/ \u30ab\u30c6\u30b4\u30ea\u5185\u306e\u8a18\u4e8b\u4e00\u89a7\u3092\u53d6\u5f97\u3059\u308b\r\n\tfunction findPosts($slug,$max=10) {\r\n\tif(0) {\r\n\r\n\t\t$sql = &lt;&lt;&lt; HERE\r\nselect *\r\nfrom wp_term_taxonomy as TermTaxonomy\r\n inner join wp_term_relationships as TermRelationship\r\n on TermTaxonomy.term_taxonomy_id = TermRelationship.term_taxonomy_id\r\n inner join wp_terms as Term\r\n  on ( Term.term_id = TermTaxonomy.term_id )\r\n inner join wp_posts as Post\r\n  on ( TermRelationship.object_id = Post.ID )\r\n  where TermTaxonomy.taxonomy = &#039;category&#039;\r\n   and Term.slug = &#039;$slug&#039;\r\n order by Post.post_date desc\r\n limit $max\r\n ;\r\nHERE;\r\n\treturn $this-&gt;query($sql);\r\n\t} else {\r\n\t\t$Post = ClassRegistry::init(&#039;Post&#039;);\r\n\t\t$items = $Post-&gt;find(&#039;all&#039;,array(\r\n\t\t\t&#039;conditions&#039;=&gt;&amp;quot;TermTaxonomy.taxonomy = &#039;category&#039;&amp;quot;));\r\n\t\treturn $items ;\r\n\t}\r\n\r\n\t}\r\n\r\n\tfunction getCategoryName($slug) {\r\n\t\t$Term = ClassRegistry::init(&#039;Term&#039;);\r\n\t\t$item = $Term-&gt;find(&#039;first&#039;,array(\r\n\t\t\t&#039;conditions&#039;=&gt;&amp;quot;slug = &#039;$slug&#039;&amp;quot;));\r\n\t\treturn $item&#x5B;&#039;Term&#039;]&#x5B;&#039;name&#039;];\r\n\t}\r\n}\r\n?&gt;\r\n<\/pre>\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<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\n&lt;!-- models\/term_relationship.php --&gt;\r\n&lt;?php\r\nclass TermRelationship extends AppModel\r\n{\r\n\tvar $name = &#039;TermRelationship&#039;;\r\n\tvar $primaryKey = array(&#039;object_id&#039;,&#039;term_taxonomy_id&#039;);\r\n\tvar $belongsTo = array(\r\n\t\t&#039;TermTaxonomy&#039; =&gt; array(\r\n\t\t\t&#039;className&#039; =&gt; &#039;TermTaxonomy&#039;,\r\n\t\t\t&#039;conditions&#039; =&gt; &#039;TermTaxonomy.term_taxonomy_id = TermRelationship.term_taxonomy_id&#039;));\r\n\tvar $hasOne = array(\r\n\t\t&#039;Post&#039; =&gt; array(\r\n\t\t\t&#039;className&#039; =&gt; &#039;Post&#039;,\r\n\t\t\t&#039;conditions&#039; =&gt; &#039;Post.ID = TermRelationship.object_id&#039;));\r\n}\r\n?&gt;\r\n<\/pre>\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<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\n&lt;!-- models\/post.php --&gt;\r\n&lt;?php\r\nclass Post extends AppModel\r\n{\r\n\tvar $name = &#039;Post&#039;;\r\n\r\n\tvar $belongsTo = array(\r\n\t    &#039;User&#039; =&gt; array(\r\n\t\t\t&#039;className&#039; =&gt; &#039;User&#039;,\r\n\t\t\t&#039;foreignKey&#039; =&gt; &#039;post_author&#039; ));\r\n\tvar $hasOne = array(\r\n\t\t&#039;TermRelationship&#039; =&gt; array(\r\n\t\t\t&#039;className&#039; =&gt; &#039;TermRelationship&#039;,\r\n\t\t\t&#039;foreignKey&#039; =&gt; &#039;object_id&#039;));\r\n}\r\n<\/pre>\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<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\n\t\/\/ \u30ab\u30c6\u30b4\u30ea\u5185\u306e\u8a18\u4e8b\u4e00\u89a7\u3092\u53d6\u5f97\u3059\u308b\r\n\tfunction findPosts($slug,$max=10) {\r\n\t\t$TermTaxonomy = ClassRegistry::init(&#039;TermTaxonomy&#039;);\r\n\t\t$items = $TermTaxonomy-&gt;find(&#039;all&#039;,array(\r\n\t\t\t&#039;conditions&#039;=&gt; array(\r\n\t\t\t\t&#039;TermTaxonomy.taxonomy&#039; =&gt; &#039;category&#039;,\r\n\t\t\t\t&#039;Post.post_status&#039; =&gt; &#039;publish&#039; )));\r\n\t\treturn $items ;\r\n\t}\r\n<\/pre>\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<pre class=\"brush: sql; title: ; notranslate\" title=\"\">\r\nSELECT\r\n `TermTaxonomy`.`term_taxonomy_id`,\r\n `TermTaxonomy`.`term_id`,\r\n `TermTaxonomy`.`taxonomy`,\r\n `TermTaxonomy`.`description`,\r\n `TermTaxonomy`.`parent`,\r\n `TermTaxonomy`.`count`,\r\n `Post`.`ID`,\r\n `Post`.`post_author`,\r\n `Post`.`post_date`,\r\n `Post`.`post_date_gmt`,\r\n `Post`.`post_content`,\r\n `Post`.`post_title`,\r\n `Post`.`post_excerpt`,\r\n `Post`.`post_status`,\r\n `Post`.`comment_status`,\r\n `Post`.`ping_status`,\r\n `Post`.`post_password`,\r\n `Post`.`post_name`,\r\n `Post`.`to_ping`,\r\n `Post`.`pinged`,\r\n `Post`.`post_modified`,\r\n `Post`.`post_modified_gmt`,\r\n `Post`.`post_content_filtered`,\r\n `Post`.`post_parent`,\r\n `Post`.`guid`,\r\n `Post`.`menu_order`,\r\n `Post`.`post_type`,\r\n `Post`.`post_mime_type`,\r\n `Post`.`comment_count`,\r\n `Term`.`term_id`,\r\n `Term`.`name`,\r\n `Term`.`slug`,\r\n `Term`.`term_group`\r\nFROM\r\n `wp_term_taxonomy` AS `TermTaxonomy`\r\n   LEFT JOIN `wp_posts` AS `Post`\r\n    ON (`TermRelationship`.`object_id` = `Post`.`ID` AND `TermTaxonomy`.`post_id` = `Post`.`id`)\r\n   LEFT JOIN `wp_terms` AS `Term`\r\n    ON (`Term`.`term_id` = `TermTaxonomy`.`term_taxonomy_id`)\r\nWHERE\r\n `TermTaxonomy`.`taxonomy` = 'category'\r\nAND `Post`.`post_status` = 'publish'\r\n<\/pre>\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_memberships_contains_paid_content":false,"footnotes":""},"categories":[20,13],"tags":[],"class_list":["post-1922","post","type-post","status-publish","format-standard","hentry","category-cakephp","category-wordpress"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_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}]}}