{"id":1799,"date":"2014-05-10T05:44:32","date_gmt":"2014-05-10T05:44:32","guid":{"rendered":"http:\/\/musilda.cz\/?p=1799"},"modified":"2014-05-10T05:44:32","modified_gmt":"2014-05-10T05:44:32","slug":"funkce-get_posts","status":"publish","type":"post","link":"https:\/\/affinite.io\/cs\/funkce-get_posts\/","title":{"rendered":"Funkce get_posts"},"content":{"rendered":"

Funkce get_posts, je jedna z hojn\u011b vyu\u017e\u00edvan\u00fdch funkc\u00ed, pro vytv\u00e1\u0159en\u00ed loop\u016f (smy\u010dek) s v\u00fdpisem \u010dl\u00e1nk\u016f. M\u016f\u017eete tak vypsat nap\u0159\u00edklad posledn\u00ed \u010dl\u00e1nky, nebo \u010dl\u00e1nky, dle zadan\u00fdch argument\u016f.<\/p>\n

Funkce get_posts se m\u016f\u017ee na prvn\u00ed pohled jevit jako podobn\u00e1 funkci get_pages, ale na rozd\u00edl od n\u00ed, nep\u0159istupuje p\u0159\u00edmo do datab\u00e1ze. Nam\u00edsto toho, vyu\u017e\u00edv\u00e1 WP_Query a ka\u017ed\u00fdm zavol\u00e1n\u00edm get_posts, tak vytvo\u0159\u00edte novou instanci objektu. D\u00edky tomu tak m\u016f\u017eete na str\u00e1nce vytvo\u0159it v\u00edce loop\u016f – v\u00fdpis\u016f \u010dl\u00e1nk\u016f, dle r\u016fzn\u00fdch parametr\u016f. Pokud tedy na hlavn\u00ed stran\u011b \u0161ablony vid\u00edte r\u016fzn\u00e9 v\u00fdpisy posledn\u00edch \u010dl\u00e1nk\u016f, \u010dl\u00e1nk\u016f od autora, nebo kategorie, m\u016f\u017eete toho dos\u00e1hnout pr\u00e1v\u011b pou\u017eit\u00edm get_posts. WP_Query pou\u017e\u00edv\u00e1 tak\u00e9 query_posts, ale nedoporu\u010duje se pou\u017e\u00edvat, pokud nechcete ovlivnit hlavn\u00ed loop na str\u00e1nce. Pokud chcete ovlivnit hlavn\u00ed v\u00fdpis, pou\u017eijte pro to hook pre_get_posts.<\/p>\n

Pou\u017eit\u00ed get_posts<\/h2>\n
$posts_array = get_posts( $args );<\/pre>\n

N\u011bkolik p\u0159\u00edklad\u016f pou\u017eit\u00ed<\/h2>\n

V\u00fdpis \u010dl\u00e1nk\u016f od jednoho autora:<\/p>\n

\n$query = get_posts( 'author_name=rami' );\n<\/pre>\n

V\u00fdpis \u010dl\u00e1nk\u016f z jedn\u00e9 kategorie podle id, nebo slug:<\/p>\n

\n$query = get_posts( 'cat=4' );\n$query = get_posts( 'category_name=staff' );\n<\/pre>\n

V\u00fdpis \u010dl\u00e1nk\u016f podle \u0161t\u00edtk\u016f:<\/p>\n

\n$query = get_posts( 'tag=bread,baking' );\n<\/pre>\n

V\u00fdpis \u010dl\u00e1nk\u016f podle taxonomie:<\/p>\n

\n$args = array(\n\t'post_type' => 'post',\n\t'people' => 'bob'\n);\n$query = get_posts( $args );\n<\/pre>\n

V\u00fdpis \u010dl\u00e1nk\u016f pro custom post type „recepty“:<\/p>\n

\n$query = get_posts( 'post_type=recepty' );\n<\/pre>\n

V praxi se v\u011bt\u0161inou s takto jednoduch\u00fdmi dotazy nesetk\u00e1te, \u010dasto se budou argumenty kombinovat a tak je b\u011b\u017en\u011bj\u0161\u00ed podobn\u00fd k\u00f3d:<\/p>\n

\n$args_search = array(\n    \t'post_type' => array( 'telefony' ),\n        'posts_per_page' => -1,\n        'meta_key' => 'vybaveni',\n        'orderby' => 'meta_value_num',\n        'order' => 'ASC'\n    );\n\n$wp_query = get_posts( $args_search );\n<\/pre>\n

Vzhledem k tomu, \u017ee argumenty i pou\u017eit\u00ed je velmi podobn\u00e9 WP_Query, m\u016f\u017eete stejn\u00e9 k\u00f3dy pou\u017e\u00edt i takto:<\/p>\n

\n$query = new WP_Query( 'tag=bread,baking' );\n<\/pre>\n

Pomoc\u00ed get_posts si tak m\u016f\u017eete v\u00fdpis \u010dl\u00e1nk\u016f upravit k obrazu sv\u00e9mu, jen se mus\u00edte rozhodnout, zda nen\u00ed pou\u017eit\u00ed zbyte\u010dn\u00e9. Pro custom post type a jejich taxonomie, m\u00e1 WordPress definov\u00e1ny soubory, slou\u017e\u00edc\u00ed pro jejich v\u00fdpis.<\/p>\n

Seznam argument\u016f<\/h2>\n
    \n
  • author<\/li>\n
  • author_name<\/li>\n
  • author__in<\/li>\n
  • author__not_in<\/li>\n
  • cat<\/li>\n
  • category_name<\/li>\n
  • category__and<\/li>\n
  • category__in<\/li>\n
  • category__not_in<\/li>\n
  • tag<\/li>\n
  • tag_id<\/li>\n
  • tag__and<\/li>\n
  • tag__in<\/li>\n
  • tag__not_in<\/li>\n
  • tag_slug__and<\/li>\n
  • tag_slug__in<\/li>\n
  • {tax}<\/li>\n
  • tax_query<\/li>\n
  • \n
      \n
    • relation\n
        \n
      • taxonomy<\/li>\n
      • field<\/li>\n
      • terms<\/li>\n
      • include_children<\/li>\n
      • operator<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<\/li>\n
      • s<\/li>\n
      • p<\/li>\n
      • name<\/li>\n
      • page_id<\/li>\n
      • pagename<\/li>\n
      • post_parent<\/li>\n
      • post_parent__in<\/li>\n
      • post_parent__not_in<\/li>\n
      • post__in<\/li>\n
      • post__not_in<\/li>\n
      • has_password<\/li>\n
      • post_password<\/li>\n
      • post_type\n
          \n
        • post<\/li>\n
        • page<\/li>\n
        • revision<\/li>\n
        • attachment<\/li>\n
        • any<\/li>\n
        • „Custom Post Types“<\/li>\n<\/ul>\n<\/li>\n
        • post_status\n
            \n
          • publish<\/li>\n
          • pending<\/li>\n
          • draft<\/li>\n
          • auto-draft<\/li>\n
          • future<\/li>\n
          • private<\/li>\n
          • inherit<\/li>\n
          • trash<\/li>\n
          • any<\/li>\n<\/ul>\n<\/li>\n
          • nopaging<\/li>\n
          • posts_per_page<\/li>\n
          • posts_per_archive_page<\/li>\n
          • offset<\/li>\n
          • paged<\/li>\n
          • page<\/li>\n
          • ignore_sticky_posts<\/li>\n
          • order\n
              \n
            • ASC<\/li>\n
            • DESC<\/li>\n<\/ul>\n<\/li>\n
            • orderby\n
                \n
              • none<\/li>\n
              • ID<\/li>\n
              • author<\/li>\n
              • title<\/li>\n
              • name<\/li>\n
              • date<\/li>\n
              • modified<\/li>\n
              • parent<\/li>\n
              • rand<\/li>\n
              • comment_count<\/li>\n
              • menu_order<\/li>\n
              • meta_value<\/li>\n
              • meta_value_num<\/li>\n
              • post__in<\/li>\n<\/ul>\n<\/li>\n
              • year<\/li>\n
              • monthnum<\/li>\n
              • w<\/li>\n
              • day<\/li>\n
              • hour<\/li>\n
              • minute<\/li>\n
              • second<\/li>\n
              • m<\/li>\n
              • date_query\n
                  \n
                • year<\/li>\n
                • month<\/li>\n
                • week<\/li>\n
                • day<\/li>\n
                • hour<\/li>\n
                • minute<\/li>\n
                • second<\/li>\n
                • after\n
                    \n
                  • year<\/li>\n
                  • month<\/li>\n
                  • day<\/li>\n<\/ul>\n<\/li>\n
                  • before\n
                      \n
                    • year<\/li>\n
                    • month<\/li>\n
                    • day<\/li>\n<\/ul>\n<\/li>\n
                    • inclusive<\/li>\n
                    • compare<\/li>\n
                    • column<\/li>\n
                    • relation<\/li>\n<\/ul>\n<\/li>\n
                    • meta_key<\/li>\n
                    • meta_value<\/li>\n
                    • meta_value_num<\/li>\n
                    • meta_compare<\/li>\n
                    • meta_query\n
                        \n
                      • relation<\/li>\n
                      • key<\/li>\n
                      • value<\/li>\n
                      • compare<\/li>\n
                      • type<\/li>\n<\/ul>\n<\/li>\n
                      • perm<\/li>\n
                      • cache_results<\/li>\n
                      • update_post_meta_cache<\/li>\n
                      • update_post_term_cache<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"

                        Funkce get_posts, je jedna z hojn\u011b vyu\u017e\u00edvan\u00fdch funkc\u00ed, pro vytv\u00e1\u0159en\u00ed loop\u016f (smy\u010dek) s v\u00fdpisem \u010dl\u00e1nk\u016f. M\u016f\u017eete tak vypsat nap\u0159\u00edklad posledn\u00ed \u010dl\u00e1nky, nebo \u010dl\u00e1nky, dle zadan\u00fdch argument\u016f.<\/p>\n","protected":false},"author":1,"featured_media":8523,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7,15],"tags":[158],"class_list":["post-1799","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-codex","category-post-funkce","tag-funkce-get_posts"],"_links":{"self":[{"href":"https:\/\/affinite.io\/cs\/wp-json\/wp\/v2\/posts\/1799"}],"collection":[{"href":"https:\/\/affinite.io\/cs\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/affinite.io\/cs\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/affinite.io\/cs\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/affinite.io\/cs\/wp-json\/wp\/v2\/comments?post=1799"}],"version-history":[{"count":0,"href":"https:\/\/affinite.io\/cs\/wp-json\/wp\/v2\/posts\/1799\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/affinite.io\/cs\/wp-json\/wp\/v2\/media\/8523"}],"wp:attachment":[{"href":"https:\/\/affinite.io\/cs\/wp-json\/wp\/v2\/media?parent=1799"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/affinite.io\/cs\/wp-json\/wp\/v2\/categories?post=1799"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/affinite.io\/cs\/wp-json\/wp\/v2\/tags?post=1799"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}