
{"id":9343,"date":"2025-05-02T21:26:10","date_gmt":"2025-05-02T12:26:10","guid":{"rendered":"https:\/\/techmania.jp\/blog\/?p=9343"},"modified":"2025-05-02T21:26:11","modified_gmt":"2025-05-02T12:26:11","slug":"wp-query_posts","status":"publish","type":"post","link":"https:\/\/techmania.jp\/blog\/wp-query_posts\/","title":{"rendered":"\u3010WordPress\u5165\u9580\u3011query_posts\u306f\u975e\u63a8\u5968\uff01\u4ee3\u66ff\u624b\u6bb5\u3068\u975e\u63a8\u5968\u306e\u7406\u7531\u3092\u5fb9\u5e95\u89e3\u8aac"},"content":{"rendered":"\n<p>WordPress\u3067\u6295\u7a3f\u8a18\u4e8b\u3092\u53d6\u5f97\u3059\u308b\u969b\u306b\u3001\u30c7\u30fc\u30bf\u306e\u53d6\u5f97\u6761\u4ef6\u3092\u72ec\u81ea\u306b\u30ab\u30b9\u30bf\u30de\u30a4\u30ba\u3057\u305f\u3044\u30b1\u30fc\u30b9\u304c\u3042\u308b\u3068\u601d\u3044\u307e\u3059\u3002<\/p>\n\n\n\n<p>\u4e0a\u8a18\u306e\u3088\u3046\u306a\u5834\u9762\u306b\u304a\u3044\u3066\u3001\u304b\u3064\u3066\u306f \u300cquery_posts()\u300d \u3092\u5229\u7528\u3059\u308b\u65b9\u6cd5\u304c\u3088\u304f\u7d39\u4ecb\u3055\u308c\u3066\u3044\u305f\u305f\u3081\u3001\u4eca\u3067\u3082\u691c\u7d22\u3059\u308b\u3068\u305d\u3046\u3044\u3063\u305f\u624b\u6cd5\u304c\u898b\u3064\u304b\u308b\u3053\u3068\u304c\u3042\u308b\u304b\u3068\u601d\u3044\u307e\u3059\u3002<\/p>\n\n\n\n<p>\u3067\u3059\u304c\u3001\u3053\u306e\u65b9\u6cd5\u306b\u306f\u69d8\u3005\u306a\u554f\u984c\u304c\u898b\u3064\u304b\u3063\u305f\u305f\u3081\u3001\u73fe\u5728\u306f\u516c\u5f0f\u306b\u975e\u63a8\u5968\u3068\u306a\u3063\u3066\u3044\u307e\u3059\u3002<\/p>\n\n\n\n<p>\u305d\u3053\u3067\u672c\u8a18\u4e8b\u3067\u306f\u3001\u975e\u63a8\u5968\u3068\u306a\u3063\u305f\u7406\u7531\u3084\u9069\u5207\u306a\u4ee3\u66ff\u624b\u6bb5\u306b\u3064\u3044\u3066\u3001\u8a73\u3057\u304f\u89e3\u8aac\u3057\u3066\u3044\u304d\u305f\u3044\u3068\u601d\u3044\u307e\u3059\u3002<\/p>\n\n\n\n<h2 id=\"outline__1\" class=\"wp-block-heading\">query_posts\u3068\u306f\uff1f<\/h2>\n\n\n\n<p>query_posts\u306f\u3001WordPress\u3067\u6295\u7a3f\u8a18\u4e8b\u306a\u3069\u3092\u8868\u793a\u3059\u308b\u306e\u306b\u4f7f\u7528\u3057\u3066\u3044\u308b\u30e1\u30a4\u30f3\u30af\u30a8\u30ea\u3092\u66f8\u304d\u63db\u3048\u308b\u30c6\u30f3\u30d7\u30ec\u30fc\u30c8\u30bf\u30b0\u3067\u3059\u3002<\/p>\n\n\n\n<p>\u30e1\u30a4\u30f3\u30af\u30a8\u30ea\u3068\u306f\u3001\u30a2\u30af\u30bb\u30b9\u5148\u306eURL\u306b\u5fdc\u3058\u3066\u81ea\u52d5\u3067\u767a\u884c\u3055\u308c\u308b\u30af\u30a8\u30ea\u306e\u3053\u3068\u3092\u6307\u3057\u307e\u3059\u3002<br>query_posts\u3092\u4f7f\u7528\u3059\u308b\u3053\u3068\u3067\u3001\u30e1\u30a4\u30f3\u30af\u30a8\u30ea\u306e\u5b9f\u884c\u6642\u306b\u8aad\u307f\u8fbc\u3080\u60c5\u5831\u3092\u4efb\u610f\u306e\u5185\u5bb9\u306b\u5909\u66f4\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002<\/p>\n\n\n\n<h2 id=\"outline__2\" class=\"wp-block-heading\">query_posts\u304c\u300c\u975e\u63a8\u5968\u300d\u306e\u7406\u7531\u306f\uff1f\u4ee3\u66ff\u624b\u6bb5\u306f\u3042\u308b\uff1f<\/h2>\n\n\n\n<p>query_posts\u306f\u3001\u516c\u5f0f\u306b\u975e\u63a8\u5968\u3068\u3055\u308c\u3066\u3044\u308b\u30c6\u30f3\u30d7\u30ec\u30fc\u30c8\u30bf\u30b0\u3067\u3082\u3042\u308a\u307e\u3059\u3002<br>\u305d\u306e\u7406\u7531\u3068\u3057\u3066\u3082\u3063\u3068\u3082\u5927\u304d\u3044\u306e\u304c\u3001\u4e0d\u5177\u5408\u306e\u767a\u751f\u3092\u5f15\u304d\u8d77\u3053\u3057\u3084\u3059\u304f\u306a\u308b\u3068\u3044\u3046\u70b9\u3067\u3059\u3002<\/p>\n\n\n\n<p>WordPress\u306b\u306f\u3001\u300c$wp_query\u300d \u3068\u3044\u3046\u30b0\u30ed\u30fc\u30d0\u30eb\u5909\u6570\u304c\u3042\u308a\u3001\u30e1\u30a4\u30f3\u30af\u30a8\u30ea\u306e\u767a\u884c\u306b\u5fc5\u8981\u306a\u60c5\u5831\u304c\u3053\u306e\u5909\u6570\u306b\u683c\u7d0d\u3055\u308c\u3066\u3044\u307e\u3059\u3002<br>query_posts\u3092\u4f7f\u7528\u3059\u308b\u3068\u3001\u3053\u306e $wp_query\u306b\u683c\u7d0d\u3055\u308c\u3066\u3044\u308b\u30af\u30a8\u30ea\u306e\u60c5\u5831\u304c\u4e0a\u66f8\u304d\u3055\u308c\u3001\u305d\u306e\u7d50\u679c\u3068\u3057\u3066\u4efb\u610f\u306e\u30c7\u30fc\u30bf\u304c\u53d6\u5f97\u3067\u304d\u308b\u3088\u3046\u306b\u306a\u308b\u4ed5\u7d44\u307f\u306b\u306a\u3063\u3066\u3044\u307e\u3059\u3002<\/p>\n\n\n\n<p>\u3067\u3059\u304c\u3001$wp_query\u306f WordPress\u5168\u4f53\u3067\u4f7f\u7528\u3055\u308c\u308b\u5909\u6570\u306e\u305f\u3081\u3001\u60c5\u5831\u3092\u66f8\u304d\u63db\u3048\u308b\u3053\u3068\u3067\u4ed6\u306e\u30da\u30fc\u30b8\u306e\u30e1\u30a4\u30f3\u30af\u30a8\u30ea\u306b\u3082\u5f71\u97ff\u3092\u53ca\u307c\u3057\u3066\u3057\u307e\u3063\u305f\u308a\u3001\u30da\u30fc\u30b8\u30cd\u30fc\u30b7\u30e7\u30f3\u304c\u6b63\u3057\u304f\u6a5f\u80fd\u3057\u306a\u304f\u306a\u308b\u3068\u3044\u3063\u305f\u4e0d\u5177\u5408\u3092\u5f15\u304d\u8d77\u3053\u3059\u53ef\u80fd\u6027\u304c\u9ad8\u304f\u306a\u308a\u307e\u3059\u3002<\/p>\n\n\n\n<p>\u305d\u306e\u4ed6\u306b\u3082\u3001query_posts\u3092\u4f7f\u7528\u3059\u308b\u3053\u3068\u3067\u5fc5\u8981\u306a\u51e6\u7406\u91cf\u304c\u5897\u3048\u308b\u5834\u5408\u304c\u3042\u308a\u3001\u30da\u30fc\u30b8\u306e\u8aad\u307f\u8fbc\u307f\u6642\u9593\u304c\u9577\u304f\u306a\u3063\u3066\u3057\u307e\u3046\u539f\u56e0\u306b\u3082\u306a\u308a\u304b\u306d\u307e\u305b\u3093\u3002<br>\u516c\u5f0f\u304c\u975e\u63a8\u5968\u3068\u3059\u308b\u901a\u308a\u306b\u3001\u3088\u307b\u3069\u306e\u7406\u7531\u304c\u306a\u3044\u9650\u308a\u306f\u4f7f\u7528\u3092\u63a7\u3048\u308b\u65b9\u304c\u5b89\u5168\u306b\u904b\u7528\u3067\u304d\u307e\u3059\u3002<\/p>\n\n\n\n<p>query_posts\u306e\u4ee3\u66ff\u624b\u6bb5\u3068\u3057\u3066\u63a8\u5968\u3055\u308c\u3066\u3044\u308b\u3082\u306e\u306b\u306f\u3001\u4ee5\u4e0b\u306e\uff13\u3064\u304c\u3042\u308a\u307e\u3059\u3002<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>pre_get_posts<\/li>\n\n\n\n<li>WP_Query<\/li>\n\n\n\n<li>get_posts<\/li>\n<\/ul>\n\n\n\n<p>\u305d\u308c\u305e\u308c\u9806\u756a\u306b\u3001\u4f7f\u3044\u65b9\u3092\u89e3\u8aac\u3057\u3066\u3044\u304d\u307e\u3059\u3002<\/p>\n\n\n\n<h3 id=\"outline__2_1\" class=\"wp-block-heading\">\u3010\u4ee3\u66ff\u624b\u6bb51\u3011pre_get_posts\u30d5\u30c3\u30af<\/h3>\n\n\n\n<p>pre_get_posts\u306f\u3001Wordpress\u306b\u7528\u610f\u3055\u308c\u3066\u3044\u308b\u30a2\u30af\u30b7\u30e7\u30f3\u30d5\u30c3\u30af\u306e\uff11\u3064\u3067\u3059\u3002<br>\u30a2\u30af\u30b7\u30e7\u30f3\u30d5\u30c3\u30af\u3068\u306f\u3001\u7279\u5b9a\u306e\u30bf\u30a4\u30df\u30f3\u30b0\u3067\u4efb\u610f\u306e\u51e6\u7406\u304c\u884c\u308f\u308c\u308b\u3088\u3046\u306b\u3059\u308b\u6a5f\u80fd\u306e\u3053\u3068\u3092\u6307\u3057\u307e\u3059\u3002<\/p>\n\n\n\n<p>\u30a2\u30af\u30b7\u30e7\u30f3\u30d5\u30c3\u30af\u3092\u4f7f\u7528\u3059\u308b\u969b\u306e\u69cb\u6587\u306f\u3001\u4ee5\u4e0b\u306e\u901a\u308a\u3067\u3059\u3002<\/p>\n\n\n\n<p><strong>\u3010\u57fa\u672c\u69cb\u6587\u3011<\/strong><\/p>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-php\" data-lang=\"PHP\" data-show-lang=\"1\"><code>add_action(&#39;\u30a2\u30af\u30b7\u30e7\u30f3\u30d5\u30c3\u30af\u540d&#39;, &#39;\u95a2\u6570\u540d&#39;);<\/code><\/pre><\/div>\n\n\n\n<p>pre_get_posts\u3092\u6307\u5b9a\u3057\u3066\u30a2\u30af\u30b7\u30e7\u30f3\u30d5\u30c3\u30af\u3092\u767b\u9332\u3059\u308b\u3068\u3001\u30e1\u30a4\u30f3\u30af\u30a8\u30ea\u3092\u53d6\u5f97\u3059\u308b\u524d\u306b\u95a2\u6570\u304c\u5b9f\u884c\u3055\u308c\u308b\u3088\u3046\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n\n\n\n<p>\u4ee5\u4e0b\u306f\u3001\u5b9f\u969b\u306b\u4f7f\u7528\u3059\u308b\u969b\u306e\u30b5\u30f3\u30d7\u30eb\u30b3\u30fc\u30c9\u3067\u3059\u3002<br>\u30a2\u30af\u30b7\u30e7\u30f3\u30d5\u30c3\u30af\u306f functions.php \u5185\u306b\u8a18\u8ff0\u3057\u307e\u3059\u3002<\/p>\n\n\n\n<p><strong>\u3010\u30b5\u30f3\u30d7\u30eb\u30b3\u30fc\u30c9\u3011<\/strong><\/p>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-php\" data-lang=\"PHP\" data-show-lang=\"1\"><code>function myPosts($query) {\n\n  if (is_admin() || !$query-&gt;is_main_query()){\n    return;\n  }\n\n  if ($query-&gt;is_home()) {\n    $query-&gt;set(&#39;post_type&#39;, &#39;post&#39;);\n    $query-&gt;set(&#39;posts_per_page&#39;, &#39;3&#39;);\n  }\n}\n\nadd_action(&#39;pre_get_posts&#39;, &#39;myPosts&#39;);\n<\/code><\/pre><\/div>\n\n\n\n<p>pre_get_posts\u306e\u5834\u5408\u3001\u7ba1\u7406\u753b\u9762\u306e\u30af\u30a8\u30ea\u3084\u30b5\u30d6\u30af\u30a8\u30ea\u306e\u53d6\u5f97\u524d\u306b\u3082\u95a2\u6570\u304c\u5b9f\u884c\u3055\u308c\u308b\u305f\u3081\u3001\u30e1\u30a4\u30f3\u30af\u30a8\u30ea\u4ee5\u5916\u3067\u60c5\u5831\u306e\u66f8\u304d\u63db\u3048\u3092\u884c\u308f\u306a\u3044\u3088\u3046\u3001\u300cis_admin()\u300d\u3068 \u300cis_main_query()\u300d \u3092\u4f7f\u7528\u3057\u3066\u30c1\u30a7\u30c3\u30af\u3092\u884c\u3046\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002<br>\u3053\u306e\u8a18\u8ff0\u304c\u7121\u3044\u3068\u3001\u7ba1\u7406\u753b\u9762\u3084\u30d7\u30ec\u30d3\u30e5\u30fc\u753b\u9762\u304c\u6b63\u5e38\u306b\u8868\u793a\u3055\u308c\u306a\u304f\u306a\u3063\u305f\u308a\u3001\u30b5\u30a4\u30c8\u306e\u4e00\u90e8\u306e\u8868\u793a\u304c\u610f\u56f3\u3057\u306a\u3044\u3082\u306e\u306b\u306a\u3063\u305f\u308a\u3068\u3001\u4e0d\u5177\u5408\u304c\u51fa\u3066\u3057\u307e\u3046\u53ef\u80fd\u6027\u304c\u3042\u308b\u305f\u3081\u3001\u5fc5\u305a\u30bb\u30c3\u30c8\u3067\u8a18\u8ff0\u3059\u308b\u3088\u3046\u306b\u3057\u307e\u3057\u3087\u3046\u3002<\/p>\n\n\n\n<p>\u30d6\u30ed\u30b0\u6295\u7a3f\u30da\u30fc\u30b8\u3067\u3042\u308c\u3070 \u300c$query-&gt;is_home()\u300d\u3001\u30a2\u30fc\u30ab\u30a4\u30d6\u30da\u30fc\u30b8\u3067\u3042\u308c\u3070 \u300c$query-&gt;is_archive()\u300d \u3068\u3044\u3063\u305f\u3088\u3046\u306b\u3001\u305d\u308c\u305e\u308c\u306e\u6761\u4ef6\u3067\u51e6\u7406\u3092\u5206\u3051\u308b\u3053\u3068\u3067\u3001\u30e1\u30a4\u30f3\u30af\u30a8\u30ea\u306b\u95a2\u3059\u308b\u8a18\u8ff0\u3092\uff11\u7b87\u6240\u306b\u307e\u3068\u3081\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002<\/p>\n\n\n\n<h3 id=\"outline__2_2\" class=\"wp-block-heading\">\u3010\u4ee3\u66ff\u624b\u6bb52\u3011WP_Query\u30af\u30e9\u30b9<\/h3>\n\n\n\n<p>WP_Query\u306f\u3001\u69d8\u3005\u306a\u30b3\u30f3\u30c6\u30f3\u30c4\u3092\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u304b\u3089\u53d6\u5f97\u3059\u308b\u305f\u3081\u306b\u4f7f\u7528\u3059\u308b\u30af\u30e9\u30b9\u3067\u3059\u3002<br>\u30e1\u30a4\u30f3\u30eb\u30fc\u30d7\u3068\u306f\u5225\u306b\u3001\u30b5\u30d6\u30eb\u30fc\u30d7\u3067\u6295\u7a3f\u4e00\u89a7\u306a\u3069\u3092\u8868\u793a\u3059\u308b\u969b\u306b\u4f7f\u3044\u307e\u3059\u3002<\/p>\n\n\n\n<p><strong>\u3010\u30b5\u30f3\u30d7\u30eb\u30b3\u30fc\u30c9\u3011<\/strong><\/p>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-php\" data-lang=\"PHP\" data-show-lang=\"1\"><code>&lt;?php\n\n\/\/ \u6761\u4ef6\u8a2d\u5b9a\n$args = Array(\n\u3000\u3000&#39;post_type&#39; =&gt; &#39;post&#39;,\n    &#39;posts_per_page&#39; =&gt; 3,\n);\n \n$query = new WP_Query( $args );\n \nif ( $query-&gt;have_posts() ) {\n    while ( $query-&gt;have_posts() ) {\n        $query-&gt;the_post();\n        \/\/ \u8868\u793a\u5185\u5bb9\u306b\u95a2\u3059\u308b\u51e6\u7406\u3092\u8a18\u8ff0\n    }\n} else {\n    \/\/ \u6295\u7a3f\u304c\u898b\u3064\u304b\u3089\u306a\u304b\u3063\u305f\u5834\u5408\u306e\u51e6\u7406\n}\n \n\/\/ \u30af\u30a8\u30ea\u306e\u30ea\u30bb\u30c3\u30c8\nwp_reset_postdata();\n \n?&gt;<\/code><\/pre><\/div>\n\n\n\n<h3 id=\"outline__2_3\" class=\"wp-block-heading\">\u3010\u4ee3\u66ff\u624b\u6bb53\u3011get_posts<\/h3>\n\n\n\n<p>get_posts\u306f\u3001\u6295\u7a3f\u8a18\u4e8b\u30c7\u30fc\u30bf\u3092\u53d6\u5f97\u3059\u308b\u969b\u306b\u4f7f\u7528\u3059\u308b\u30c6\u30f3\u30d7\u30ec\u30fc\u30c8\u30bf\u30b0\u3067\u3059\u3002<br>WP_Query\u30af\u30e9\u30b9\u3068\u540c\u69d8\u306b\u3001\u30b5\u30d6\u30eb\u30fc\u30d7\u3067\u6295\u7a3f\u4e00\u89a7\u306a\u3069\u3092\u8868\u793a\u3059\u308b\u5834\u5408\u306b\u4f7f\u3044\u307e\u3059\u3002<\/p>\n\n\n\n<p>get_posts\u3092\u4f7f\u7528\u3059\u308b\u3068\u3001\u6295\u7a3f\u8a18\u4e8b\u306b\u95a2\u3059\u308b\u60c5\u5831\u304c\u683c\u7d0d\u3055\u308c\u305f\u914d\u5217\u304c\u8fd4\u5374\u3055\u308c\u307e\u3059\u3002<\/p>\n\n\n\n<p><strong>\u3010\u30b5\u30f3\u30d7\u30eb\u30b3\u30fc\u30c9\u3011<\/strong><\/p>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-php\" data-lang=\"PHP\" data-show-lang=\"1\"><code>&lt;?php\n\n\/\/ \u6761\u4ef6\u8a2d\u5b9a\n$args = Array(\n        &#39;post_type&#39; =&gt; &#39;post&#39;,\n        &#39;posts_per_page&#39; =&gt; 3,\n);\n \n$my_posts = get_posts( $args );\n \nforeach ( $my_posts as $post ) {\n\u3000\u3000setup_postdata( $post );\n        \/\/ \u8868\u793a\u5185\u5bb9\u306b\u95a2\u3059\u308b\u51e6\u7406\u3092\u8a18\u8ff0\n}\n \n\/\/ \u30af\u30a8\u30ea\u306e\u30ea\u30bb\u30c3\u30c8\nwp_reset_postdata();\n \n?&gt;<\/code><\/pre><\/div>\n\n\n\n<p>get_posts\u3092\u4f7f\u7528\u3059\u308b\u5834\u5408\u306f\u3001\u300c$query->the_post()\u300d \u306b\u76f8\u5f53\u3059\u308b\u6587\u3068\u3057\u3066 \u300csetup_postdata()\u300d \u3092\u8a18\u8ff0\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002<\/p>\n\n\n\n<h2 id=\"outline__3\" class=\"wp-block-heading\">\u3069\u306e\u4ee3\u66ff\u624b\u6bb5\u3092\u4f7f\u3046\u3079\u304d\uff1f<\/h2>\n\n\n\n<p>\u3053\u3053\u307e\u3067\uff13\u3064\u306e\u4ee3\u66ff\u624b\u6bb5\u306b\u3064\u3044\u3066\u89e3\u8aac\u3092\u3057\u3066\u304d\u307e\u3057\u305f\u304c\u3001\u305d\u308c\u305e\u308c\u4f7f\u7528\u3067\u304d\u308b\u5834\u9762\u3084\u6d3b\u7528\u3067\u304d\u308b\u30c7\u30fc\u30bf\u5185\u5bb9\u306b\u306f\u9055\u3044\u304c\u3042\u308a\u307e\u3059\u3002<\/p>\n\n\n\n<p>\u307e\u305a\u524d\u63d0\u3068\u3057\u3066\u3001\u30e1\u30a4\u30f3\u30eb\u30fc\u30d7\u3067\u4f7f\u7528\u3059\u308b\u5834\u5408\u306f \u300cpre_get_posts\u300d\u3001\u30b5\u30d6\u30eb\u30fc\u30d7\u306e\u5834\u5408\u306f\u300cWP_Query\u300d \u3082\u3057\u304f\u306f \u300cget_posts\u300d \u3068\u4f7f\u3044\u5206\u3051\u308b\u3068\u3044\u3044\u3067\u3057\u3087\u3046\u3002<\/p>\n\n\n\n<p>WP_Query\u3084 get_posts\u3092\u30e1\u30a4\u30f3\u30eb\u30fc\u30d7\u3067\u4f7f\u7528\u3059\u308b\u5834\u5408\u3001\u51e6\u7406\u9806\u306e\u95a2\u4fc2\u306b\u3088\u308a\u3001\u65e2\u306b\u30e1\u30a4\u30f3\u30af\u30a8\u30ea\u3067\u30c7\u30fc\u30bf\u3092\u53d6\u5f97\u3057\u305f\u72b6\u614b\u304b\u3089\u518d\u5ea6\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u306b\u30a2\u30af\u30bb\u30b9\u3059\u308b\u3053\u3068\u306b\u306a\u308b\u305f\u3081\u3001\u30da\u30fc\u30b8\u3092\u8868\u793a\u3059\u308b\u307e\u3067\u306e\u6642\u9593\u304c\u9045\u304f\u306a\u3063\u3066\u3057\u307e\u3046\u53ef\u80fd\u6027\u304c\u3042\u308a\u307e\u3059\u3002<\/p>\n\n\n\n<p>\u53cd\u5bfe\u306b\u3001pre_get_posts\u306f\u524d\u8ff0\u306e\u7406\u7531\u306b\u3088\u308a\u3001\u30b5\u30d6\u30eb\u30fc\u30d7\u3067\u306e\u4f7f\u7528\u306b\u306f\u5371\u967a\u304c\u4f34\u3044\u3084\u3059\u3044\u4ed5\u7d44\u307f\u3068\u306a\u3063\u3066\u3044\u307e\u3059\u3002<br>\u307e\u305f\u3001\u540c\u3058\u304f\u51e6\u7406\u9806\u306a\u3069\u306e\u95a2\u4fc2\u304b\u3089\u3001\u56fa\u5b9a\u30da\u30fc\u30b8\u3067\u4f7f\u7528\u3059\u308b\u3053\u3068\u306f\u3067\u304d\u307e\u305b\u3093\u306e\u3067\u3001\u30e1\u30a4\u30f3\u30eb\u30fc\u30d7\u3067\u306e\u307f\u4f7f\u7528\u3059\u308b\u3088\u3046\u306b\u3057\u307e\u3057\u3087\u3046\u3002<\/p>\n\n\n\n<p>\u30b5\u30d6\u30eb\u30fc\u30d7\u3067\u4f7f\u7528\u53ef\u80fd\u306aWP_Query\u3068 get_posts\u306e\u9055\u3044\u306b\u3064\u3044\u3066\u3067\u3059\u304c\u3001\u53d6\u5f97\u3067\u304d\u308b\u30c7\u30fc\u30bf\u5185\u5bb9\u306e\u591a\u3055\u306b\u5dee\u304c\u3042\u308a\u307e\u3059\u3002<\/p>\n\n\n\n<p>WP_Query\u30af\u30e9\u30b9\u306f\u3001\u30e1\u30a4\u30f3\u30af\u30a8\u30ea\u306e\u53d6\u5f97\u306b\u3082\u4f7f\u7528\u3055\u308c\u3066\u3044\u308b\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u30af\u30e9\u30b9\u3067\u3059\u3002<br>\u6295\u7a3f\u8a18\u4e8b\u4ee5\u5916\u306b\u3082\u56fa\u5b9a\u30da\u30fc\u30b8\u306a\u3069\u306e\u30c7\u30fc\u30bf\u60c5\u5831\u304c\u53d6\u5f97\u3067\u304d\u305f\u308a\u3001\u30da\u30fc\u30b8\u306e\u7a2e\u985e\u3084\u30ab\u30c6\u30b4\u30ea\u540d\u306a\u3069\u306e\u69d8\u3005\u306a\u30af\u30a8\u30ea\u60c5\u5831\u304c\u4f7f\u7528\u3067\u304d\u305f\u308a\u3068\u3001\u6271\u3048\u308b\u30c7\u30fc\u30bf\u306e\u7a2e\u985e\u306b\u5e45\u304c\u3042\u308a\u307e\u3059\u3002<\/p>\n\n\n\n<p>\u4e00\u65b9\u3001get_posts \u3067\u53d6\u5f97\u3067\u304d\u308b\u30c7\u30fc\u30bf\u306f\u6295\u7a3f\u8a18\u4e8b\u306b\u95a2\u3059\u308b\u60c5\u5831\u306e\u307f\u306b\u306a\u308a\u307e\u3059\u3002<br>get_posts\u306e\u8a73\u3057\u3044\u51e6\u7406\u5185\u5bb9\u3092\u8abf\u3079\u308b\u3068\u5206\u304b\u308b\u306e\u3067\u3059\u304c\u3001\u6700\u7d42\u7684\u306b\u4ee5\u4e0b\u306e\u5f62\u3067\u623b\u308a\u5024\u304c\u8fd4\u5374\u3055\u308c\u307e\u3059\u3002<\/p>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-php\" data-lang=\"PHP\" data-show-lang=\"1\"><code>$get_posts = new WP_Query;\nreturn $get_posts-&gt;query($r);<\/code><\/pre><\/div>\n\n\n\n<p>\u3053\u306e\u30b3\u30fc\u30c9\u304c\u793a\u3059\u901a\u308a\u3001get_posts\u306f WP_Query\u30af\u30e9\u30b9\u3092\u4f7f\u7528\u3057\u3066\u53d6\u5f97\u3067\u304d\u308b\u30c7\u30fc\u30bf\u306e\u4e00\u90e8\u306e\u307f\u3092\u8fd4\u5374\u3059\u308b\u30c6\u30f3\u30d7\u30ec\u30fc\u30c8\u30bf\u30b0\u306a\u306e\u3067\u3059\u3002<br>\u30b3\u30fc\u30c9\u306e\u30b7\u30f3\u30d7\u30eb\u3055\u3084\u51e6\u7406\u901f\u5ea6\u306e\u9762\u3067\u306f get_posts\u306e\u65b9\u304c\u512a\u4f4d\u3067\u3059\u304c\u3001\u6295\u7a3f\u8a18\u4e8b\u4ee5\u5916\u306e\u30c7\u30fc\u30bf\u3092\u53d6\u5f97\u3057\u305f\u3044\u5834\u5408\u3084\u3001\u3088\u308a\u8907\u96d1\u306a\u60c5\u5831\u3092\u6271\u3044\u305f\u3044\u5834\u5408\u306b\u306f WP_Query\u3092\u4f7f\u7528\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002<\/p>\n\n\n\n<h2 id=\"outline__4\" class=\"wp-block-heading\">\u307e\u3068\u3081<\/h2>\n\n\n\n<p>\u4eca\u56de\u306f\u3001query_posts\u304c\u975e\u63a8\u5968\u3068\u306a\u3063\u3066\u3044\u308b\u7406\u7531\u3068\u4ee3\u66ff\u65b9\u6cd5\u306b\u3064\u3044\u3066\u89e3\u8aac\u3057\u307e\u3057\u305f\u3002<br>\u30b5\u30a4\u30c8\u3092\u5b89\u5168\u306b\u904b\u7528\u3059\u308b\u305f\u3081\u306b\u3082\u3001query_posts\u306f\u4f7f\u7528\u305b\u305a\u306b\u3001\u4ed6\u306e\u65b9\u6cd5\u3067\u30c7\u30fc\u30bf\u306e\u53d6\u5f97\u3092\u884c\u3046\u3088\u3046\u306b\u3057\u307e\u3057\u3087\u3046\u3002<\/p>\n\n\n\n<h2 id=\"outline__5\" class=\"wp-block-heading\">PHP\u306e\u52c9\u5f37\u65b9\u6cd5\u306f\uff1f<\/h2>\n\n\n<p>\u66f8\u7c4d\u3084\u30a4\u30f3\u30bf\u30fc\u30cd\u30c3\u30c8\u3067\u5b66\u7fd2\u3059\u308b\u65b9\u6cd5\u304c\u3042\u308a\u307e\u3059\u3002\u6628\u4eca\u3067\u306f\u3001YouTube\u306a\u3069\u306e\u52d5\u753b\u30b5\u30a4\u30c8\u3084\u30a8\u30f3\u30b8\u30cb\u30a2\u306e\u30b3\u30df\u30e5\u30cb\u30c6\u30a3\u30b5\u30a4\u30c8\u306a\u3069\u3082\u5145\u5b9f\u3057\u3066\u3044\u3066\u591a\u304f\u306e\u60c5\u5831\u304c\u624b\u306b\u5165\u308a\u307e\u3059\u3002<br \/>\u305d\u3057\u3066\u3001\u3088\u308a\u52b9\u7387\u7684\u306b\u77e5\u8b58\u30fb\u30b9\u30ad\u30eb\u3092\u7fd2\u5f97\u3059\u308b\u306b\u306f\u3001\u77e5\u8b58\u3092\u3064\u3051\u306a\u304c\u3089\u5b9f\u969b\u306b\u624b\u3092\u52d5\u304b\u3057\u3066\u307f\u308b\u306a\u3069\u3001\u30a4\u30f3\u30d7\u30c3\u30c8\u3068\u30a2\u30a6\u30c8\u30d7\u30c3\u30c8\u3092\u7e70\u308a\u8fd4\u3057\u3066\u3044\u304f\u3053\u3068\u304c\u91cd\u8981\u3067\u3059\u3002\u7279\u306b\u72ec\u5b66\u306e\u5834\u5408\u306f\u3001\u6709\u8b58\u8005\u306b\u8cea\u554f\u304c\u3067\u304d\u305f\u308a\u30d5\u30a3\u30fc\u30c9\u30d0\u30c3\u30af\u3092\u3082\u3089\u3048\u308b\u3088\u3046\u306a\u74b0\u5883\u304c\u3042\u308b\u3068\u3001\u7406\u89e3\u5ea6\u304c\u6df1\u307e\u308b\u3067\u3057\u3087\u3046\u3002<\/p>\n<p>\u305f\u3060\u3001PHP\u306b\u9650\u3089\u305a\u3001IT\u30b9\u30ad\u30eb\u3092\u8eab\u306b\u3064\u3051\u308b\u969b\u3001\u3069\u3046\u3057\u3066\u3082\u8ab2\u984c\u306b\u3076\u3064\u304b\u3063\u3066\u3057\u307e\u3046\u3053\u3068\u306f\u3042\u308a\u307e\u3059\u3088\u306d\u3002\u7279\u306b\u72ec\u5b66\u3060\u3068\u3001\u308f\u304b\u3089\u306a\u3044\u90e8\u5206\u3092\u30d7\u30ed\u306b\u8cea\u554f\u3067\u304d\u308b\u6a5f\u4f1a\u3092\u78ba\u4fdd\u3057\u306b\u304f\u304f\u3001\u30e2\u30c1\u30d9\u30fc\u30b7\u30e7\u30f3\u304c\u7d9a\u304d\u306b\u304f\u3044\u3068\u3044\u3046\u5074\u9762\u304c\u3042\u308a\u307e\u3059\u3002\u72ec\u5b66\u3067\u30e2\u30c1\u30d9\u30fc\u30b7\u30e7\u30f3\u3092\u7dad\u6301\u3059\u308b\u81ea\u4fe1\u304c\u306a\u3044\u4eba\u306b\u306f\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u30b9\u30af\u30fc\u30eb\u3068\u3044\u3046\u624b\u3082\u3042\u308a\u307e\u3059\u3002\u8cbb\u7528\u306f\u639b\u304b\u308a\u307e\u3059\u304c\u3001\u305d\u306e\u5206\u30b9\u30ad\u30eb\u3092\u8eab\u306b\u3064\u3051\u3084\u3059\u3044\u3067\u3059\u3002\u3057\u3063\u304b\u308a\u3068\u77e5\u8b58\u30fb\u30b9\u30ad\u30eb\u3092\u7fd2\u5f97\u3057\u3066\u5b9f\u8df5\u306b\u6d3b\u304b\u3057\u305f\u3044\u3068\u3044\u3046\u4eba\u306f\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u30b9\u30af\u30fc\u30eb\u304c\u304a\u3059\u3059\u3081\u3067\u3059\u3002<\/p>\n<p><span class=\"markerYellow\"><strong>\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u30b9\u30af\u30fc\u30eb\u306a\u3089\u30c6\u30c3\u30af\u30de\u30cb\u30a2\u304c\u304a\u3059\u3059\u3081\uff01<\/strong><\/span><\/p>\n\n\n<img decoding=\"async\" src=\"https:\/\/web-img.rensa.jp.net\/rensa\/images\/techmania\/thumbnail\/images\/yuudou.png\" class=\"post-img\">\n\n\n<p>IT\u30b9\u30ad\u30eb\u9700\u8981\u306e\u9ad8\u307e\u308a\u3068\u3068\u3082\u306b\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u30b9\u30af\u30fc\u30eb\u3082\u5897\u3048\u307e\u3057\u305f\u3002\u3057\u304b\u3057\u3001\u3069\u306e\u30b9\u30af\u30fc\u30eb\u306b\u901a\u3046\u3079\u304d\u304b\u8ff7\u3063\u3066\u3057\u307e\u3046\u4eba\u3082\u3044\u308b\u3067\u3057\u3087\u3046\u3002\u305d\u3093\u306a\u65b9\u306b\u306f\u30c6\u30c3\u30af\u30de\u30cb\u30a2\u3092\u304a\u3059\u3059\u3081\u3057\u307e\u3059\uff01\u3053\u308c\u307e\u3067\u591a\u304f\u306eIT\u30a8\u30f3\u30b8\u30cb\u30a2\u3092\u80b2\u6210\u30fb\u8f29\u51fa\u3057\u3066\u304d\u305f\u30c6\u30c3\u30af\u30de\u30cb\u30a2\u3067\u3082\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u30b9\u30af\u30fc\u30eb\u3092\u958b\u8b1b\u3057\u3066\u3044\u307e\u3059\u3002<\/p>\n<div class=\"bgBox\">\n<p><strong>\uff1c\u30c6\u30c3\u30af\u30de\u30cb\u30a2\u306e\u7279\u5fb4\uff1e<\/strong><br \/><strong>\u30fb\u305f\u3057\u304b\u306a\u80b2\u6210\u5b9f\u7e3e\u3068\u89aa\u8eab\u306a\u6559\u80b2\u3000\uff5e\u30bb\u30ab\u30f3\u30c9\u30ad\u30e3\u30ea\u30a2\u3092\u5168\u529b\u652f\u63f4\uff5e<\/strong><br \/><strong>\u30fb\u8b1b\u5e2b\u304c\u73fe\u5f79\u30a8\u30f3\u30b8\u30cb\u30a2\u3000\uff5e\u201c\u672c\u5f53\u201d\u306e\u958b\u767a\u30ce\u30a6\u30cf\u30a6\u3092\u76f4\u306b\u5b66\u3079\u308b\uff5e<\/strong><br \/><strong>\u30fb\u5c02\u5c5e\u8b1b\u5e2b\u304c\u5b66\u7fd2\u3092\u5fb9\u5e95\u30b5\u30dd\u30fc\u30c8\u3000\uff5e\u300c\u308f\u304b\u3089\u306a\u3044\u300d\u3092\u5fb9\u5e95\u89e3\u6c7a\uff5e<\/strong><br \/><strong>\u30fb\u5b9f\u52d9\u30d9\u30fc\u30b9\u3067\u30b9\u30ad\u30eb\u3092\u7fd2\u5f97\u3000\uff5e\u5b9f\u8df5\u7684\u306a\u51dd\u7e2e\u30ab\u30ea\u30ad\u30e5\u30e9\u30e0\uff5e<\/strong><\/p>\n<\/div>\n<p>\u3053\u306e\u3088\u3046\u306a\u7279\u5fb4\u3092\u6301\u3064\u30c6\u30c3\u30af\u30de\u30cb\u30a2\u306fIT\u30a8\u30f3\u30b8\u30cb\u30a2\u306e\u30b9\u30bf\u30fc\u30c8\u30e9\u30a4\u30f3\u3068\u3057\u3066\u6700\u9069\u3067\u3059\u3002<br \/>\u8a71\u3092\u805e\u304d\u305f\u3044\u30fb\u8a73\u3057\u304f\u77e5\u308a\u305f\u3044\u3068\u3044\u3046\u65b9\u306f<a href=\"https:\/\/techmania.jp\/lp\/2\" target=\"_blank\" rel=\"noopener\">\u3053\u3061\u3089<\/a>\u304b\u3089\u304a\u6c17\u8efd\u306b\u304a\u554f\u3044\u5408\u308f\u305b\u304f\u3060\u3055\u3044\u3002<\/p>","protected":false},"excerpt":{"rendered":"WordPress\u3067\u6295\u7a3f\u8a18\u4e8b\u3092\u53d6\u5f97\u3059\u308b\u969b\u306b\u3001\u30c7\u30fc\u30bf\u306e\u53d6\u5f97\u6761\u4ef6\u3092\u72ec\u81ea\u306b\u30ab\u30b9\u30bf\u30de\u30a4\u30ba\u3057\u305f\u3044\u30b1\u30fc\u30b9\u304c\u3042\u308b\u3068\u601d\u3044\u307e\u3059\u3002 \u4e0a\u8a18\u306e\u3088\u3046\u306a\u5834\u9762\u306b\u304a\u3044\u3066\u3001\u304b\u3064\u3066\u306f \u300cquery_posts()\u300d \u3092\u5229\u7528\u3059\u308b\u65b9\u6cd5\u304c\u3088\u304f\u7d39\u4ecb\u3055\u308c\u3066\u3044\u305f\u305f\u3081\u3001\u4eca\u3067\u3082\u691c\u7d22\u3059\u308b\u3068\u305d\u3046\u3044\u3063\u305f\u624b\u6cd5\u304c\u898b\u3064\u304b\u308b\u3053\u3068\u304c\u3042\u308b\u304b\u3068\u601d\u3044\u307e\u3059\u3002 \u3067\u3059\u304c\u3001\u3053\u306e\u65b9\u6cd5\u306b\u306f\u69d8\u3005\u306a\u554f\u984c\u304c\u898b\u3064\u304b\u3063\u305f\u305f\u3081\u3001\u73fe\u5728\u306f\u516c\u5f0f\u306b\u975e\u63a8\u5968\u3068\u306a\u3063\u3066\u3044\u307e\u3059\u3002 \u305d\u3053\u3067\u672c\u8a18\u4e8b\u3067\u306f\u3001\u975e\u63a8\u5968\u3068\u306a\u3063 [&hellip;]","protected":false},"author":11,"featured_media":9387,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"fifu_image_url":"https:\/\/web-img.rensa.jp.net\/rensa\/images\/techmania\/thumbnail\/images\/query_posts.png","fifu_image_alt":"\u3010WordPress\u5165\u9580\u3011query_posts\u306f\u975e\u63a8\u5968\uff01\u4ee3\u66ff\u624b\u6bb5\u3068\u975e\u63a8\u5968\u306e\u7406\u7531\u3092\u5fb9\u5e95\u89e3\u8aac","footnotes":""},"categories":[15],"tags":[],"class_list":["post-9343","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-programming"],"_links":{"self":[{"href":"https:\/\/techmania.jp\/blog\/wp-json\/wp\/v2\/posts\/9343","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/techmania.jp\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/techmania.jp\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/techmania.jp\/blog\/wp-json\/wp\/v2\/users\/11"}],"replies":[{"embeddable":true,"href":"https:\/\/techmania.jp\/blog\/wp-json\/wp\/v2\/comments?post=9343"}],"version-history":[{"count":3,"href":"https:\/\/techmania.jp\/blog\/wp-json\/wp\/v2\/posts\/9343\/revisions"}],"predecessor-version":[{"id":9347,"href":"https:\/\/techmania.jp\/blog\/wp-json\/wp\/v2\/posts\/9343\/revisions\/9347"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/techmania.jp\/blog\/wp-json\/wp\/v2\/media\/9387"}],"wp:attachment":[{"href":"https:\/\/techmania.jp\/blog\/wp-json\/wp\/v2\/media?parent=9343"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/techmania.jp\/blog\/wp-json\/wp\/v2\/categories?post=9343"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/techmania.jp\/blog\/wp-json\/wp\/v2\/tags?post=9343"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}