• 赤色のリンクは、まだ日本語Codexに存在しないページ・画像です。英語版と併せてご覧ください。(詳細

このWikiはいつでも誰でも編集できます

「テンプレートタグ/query posts」の版間の差分

提供: WordPress Codex 日本語版
移動先: 案内検索
(引数(パラメータ)を 2.9 時点での最新情報に更新(81060版)。それ以外の部分は原文とは合わせていない。)
(後半に en:Template Tags/query_posts 14:31, 27 February 2010 Scribu 版を翻訳用に挿入、未翻訳)
1行目: 1行目:
 +
{{NeedTrans|[[#Description|後半]]が|
 +
* <code>wp_reset_query</code>や用例などの情報も読めるよう、英語版を後半に挿入しました。前半にない情報を中心に、一部でも翻訳できる方がいたらお願いします。
 +
* 前半も簡潔で見やすいと思うのですが、どうマージしたらいいでしょう。。}}
 +
 
<!-- 2.9 時点でパラメータは最新のものに更新。 -->
 
<!-- 2.9 時点でパラメータは最新のものに更新。 -->
 
* 書式: <?php query_posts('arguments'); ?>  
 
* 書式: <?php query_posts('arguments'); ?>  
107行目: 111行目:
 
</ul></nowiki></pre>
 
</ul></nowiki></pre>
  
{{原文|Template_Tags/query_posts}}
+
----
 +
 
 +
==Description <span id="Description"></span>==
 +
<tt>Query_posts</tt> can be used to control which posts show up in [[The Loop]].  It accepts a variety of parameters in the same format as used in your URL (e.g. <tt>p=4</tt> to show only post of ID number 4).
 +
 
 +
Why go through all the trouble of changing the query that was meticulously created from your given URL?  You can customize the presentation of your blog entries by combining it with page logic (like the [[Conditional Tags]]) -- all without changing any of the URLs.
 +
 
 +
Common uses might be to:
 +
*  Display only a single post on your homepage (a single Page can be done via Settings -> Reading).
 +
*  Show all posts from a particular time period.
 +
*  Show the latest post (only) on the front page.
 +
*  Change how posts are ordered.
 +
*  Show posts from only one category.
 +
*  Exclude one or more categories.
 +
 
 +
==Important note <span id="Important_note"></span>==
 +
The query_posts function is intended to be used to modify the main page Loop ''only''. It is not intended as a means to create secondary Loops on the page. If you want to create separate Loops outside of the main one, you should create separate [[関数リファレンス/WP Query|WP_Query]] objects and use those instead. Use of query_posts on Loops other than the main one can result in your main Loop becoming incorrect and possibly displaying things that you were not expecting.
 +
 
 +
The query_posts function overrides and replaces the main query for the page. To save your sanity, do not use it for any other purpose.
 +
 
 +
==Usage <span id="">Usage</span>==
 +
 
 +
<pre>
 +
<?php
 +
 
 +
//The Query
 +
query_posts('posts_per_page=5');
 +
 
 +
//The Loop
 +
if ( have_posts() ) : while ( have_posts() ) : the_post();
 +
..
 +
endwhile; else:
 +
..
 +
endif;
 +
 
 +
//Reset Query
 +
wp_reset_query();
 +
 
 +
?>
 +
</pre>
 +
 
 +
===Usage Note <span id="Usage_Note"></span>===
 +
 
 +
Place a call to <tt>query_posts()</tt> in one of your [[テンプレート|Template]] files before [[The Loop]] begins. The <tt>wp_query</tt> object will generate a new SQL query using your parameters. When you do this, WordPress ignores the other parameters it receives via the URL (such as page number or category). If you want to preserve that information, you can use the <tt>$query_string</tt> global variable in the call to <tt>query_posts()</tt>.
 +
 
 +
For example, to set the display order of the posts without affecting the rest of the query string, you could place the following before [[The Loop]]:
 +
 
 +
<pre>
 +
global $query_string;
 +
query_posts($query_string . "&order=ASC");
 +
</pre>
 +
 
 +
When using <tt>query_posts</tt> in this way, the quoted portion of the argument ''must'' begin with an ampersand (&).
 +
 
 +
 
 +
==Parameters <span id=""></span>==
 +
This is not an exhaustive list yet. It is meant to show some of the more common things possible with setting your own queries.
 +
 
 +
===Category Parameters <span id="Category_Parameters"></span>===
 +
Show posts only belonging to certain categories.
 +
*  <tt>cat</tt> - must use cat ids
 +
*  <tt>category_name</tt>
 +
*  <tt>category__and</tt> - must use cat ids
 +
*  <tt>category__in</tt> - must use cat ids
 +
*  <tt>category__not_in</tt> - must use cat ids
 +
 
 +
'''Show One Category by ID'''
 +
 
 +
Display posts from only one category ID (and any children of that category):
 +
 
 +
query_posts('cat=4');
 +
 
 +
'''Show One Category by Name'''
 +
 
 +
Display posts from only one category by name:
 +
 
 +
query_posts('category_name=Staff Home');
 +
 
 +
'''Show Several Categories by ID'''
 +
 
 +
Display posts from several specific category IDs:
 +
 
 +
query_posts('cat=2,6,17,38');
 +
 
 +
'''Exclude Posts Belonging to Only One Category'''
 +
 
 +
Show all posts ''except'' those from a category by prefixing its ID with a '-' (minus) sign.
 +
 
 +
query_posts('cat=-3');
 +
 
 +
This excludes any post that belongs to category 3. <!--There is a proviso however: it will exclude all the posts that belong ''only'' to category 3. If a post belongs to another category as well, it will still be picked up.-->
 +
 
 +
'''Multiple Category Handling'''
 +
 
 +
Display posts that are in multiple categories.  This shows posts that are in both categories 2 and 6:
 +
 
 +
query_posts(array('category__and' => array(2,6)));
 +
 
 +
To display posts from either category 2 OR 6, you could use <tt>cat</tt> as mentioned above, or by using <tt>category__in</tt> (note this does not show posts from any children of these categories):
 +
 
 +
query_posts(array('category__in' => array(2,6)));
 +
 
 +
You can also exclude multiple categories this way:
 +
 
 +
query_posts(array('category__not_in' => array(2,6)));
 +
 
 +
===Tag Parameters <span id="Tag_Parameters"></span>===
 +
Show posts associated with certain tags.
 +
*  <tt>tag</tt>
 +
*  <tt>tag_id</tt> - must use tag ids
 +
*  <tt>tag__and</tt> - must use tag ids
 +
*  <tt>tag__in</tt> - must use tag ids
 +
*  <tt>tag__not_in</tt> - must use tag ids
 +
*  <tt>tag_slug__and</tt>
 +
*  <tt>tag_slug__in</tt>
 +
 
 +
'''Fetch posts for one tag'''
 +
query_posts('tag=cooking');
 +
 
 +
'''Fetch posts that have either of these tags'''
 +
 
 +
query_posts('tag=bread,baking');
 +
 
 +
'''Fetch posts that have all three of these tags:
 +
 
 +
query_posts('tag=bread+baking+recipe');
 +
 
 +
'''Multiple Tag Handling'''
 +
 
 +
Display posts that are tagged with both tag id 37 and tag id 47:
 +
 
 +
query_posts(array('tag__and' => array(37,47));
 +
 
 +
To display posts from either tag id 37 or 47, you could use <tt>tag</tt> as mentioned above, or explicitly specify by using <tt>tag__in</tt>:
 +
 
 +
query_posts(array('tag__in' => array(37,47));
 +
 
 +
Display posts that do not have any of the two tag ids 37 and 47:
 +
 
 +
query_posts(array('tag__not_in' => array(37,47));
 +
 
 +
The <tt>tag_slug__in</tt> and <tt>tag_slug__and</tt> behave much the same, except match against the tag's slug.
 +
 
 +
Also see [http://boren.nu/archives/2007/10/01/taxonomy-intersections-and-unions/ Ryan's discussion of Tag intersections and unions].
 +
 
 +
===Author Parameters <span id="Author_Parameters"></span>===
 +
You can also restrict the posts by author.
 +
*  <tt>author=3</tt>
 +
*  <tt>author=-3</tt> - exclude author id 3 posts
 +
*  <tt>author_name=Harriet</tt>
 +
 
 +
'''Note:''' <tt>author_name</tt> operates on the <tt>user_nicename</tt> field, whilst <tt>author</tt> operates on the author id field.
 +
 
 +
'''Display all [[Pages]] for author=1, in title order, with no sticky posts tacked to the top:'''
 +
query_posts('caller_get_posts=1&author=1&post_type=page&post_status=publish&orderby=title&order=ASC');
 +
 
 +
===Post & Page Parameters <span id="Post_.26_Page_Parameters"></span>===
 +
 
 +
Retrieve a single post or page.
 +
 
 +
*  <tt>'p' => 27</tt> - use the post ID to show that post
 +
*  <tt>'name' => 'about-my-life'</tt> - query for a particular post that has this [[Introduction to Blogging#Post Slugs|Post Slug]]
 +
*  <tt>'page_id' => 7</tt> - query for just Page ID 7
 +
*  <tt>'pagename' => 'about'</tt> - note that this is not the page's title, but the page's path
 +
*  <tt>'posts_per_page' => 1</tt> - use <tt>'posts_per_page' => 3</tt> to show 3 posts. Use <tt>'posts_per_page' => -1</tt> to show all posts
 +
*  <tt>'showposts' => 1</tt> - use <tt>'showposts' => 3</tt> to show 3 posts. Use <tt>'showposts' => -1</tt> to show all posts.  '''Deprecated in favor of posts_per_page'''
 +
*  <tt>'post__in' => array(5,12,2,14,7)</tt> - inclusion, lets you specify the post IDs to retrieve
 +
*  <tt>'post__not_in' => array(6,2,8)</tt> - exclusion, lets you specify the post IDs NOT to retrieve
 +
*  <tt>'post_type' => 'page'</tt> - returns [[Pages]]; defaults to value of <tt>post</tt>; can be <tt>any</tt>, <tt>attachment</tt>, <tt>page</tt>, <tt>post</tt>, or <tt>revision</tt>. <tt>any</tt> retrieves any type except revisions.
 +
*  <tt>'post_status' => 'publish'</tt> - returns publish works.  Also could use <tt>pending</tt>, <tt>draft</tt>, <tt>future</tt>, <tt>private</tt>, <tt>trash</tt>.  For <tt>inherit</tt> see [[関数リファレンス/get_children|get_children]]/[[:en:Function Reference/get_children|en]].  Status of <tt>trash</tt> added with [[Version 2.9]].
 +
*  <tt>'post_parent' => 93</tt> - return just the child Pages of Page 93.
 +
 
 +
===Sticky Post Parameters <span id="Sticky_Post_Parameters"></span>===
 +
Sticky posts first became available with WordPress Version 2.7.  Posts that are set as Sticky will be displayed before other posts in a query, unless excluded with the ''caller_get_posts=1'' parameter.
 +
* <tt>array('post__in'=>get_option('sticky_posts'))</tt> - returns array of all sticky posts
 +
* <tt>caller_get_posts=1</tt> - To exclude sticky posts being included at the beginning of posts returned, but the sticky post will still be returned in the natural order of that list of posts returned.
 +
 
 +
'''To return just the first sticky post''':
 +
$sticky=get_option('sticky_posts') ;
 +
query_posts('p=' . $sticky[0]);
 +
 
 +
or
 +
 
 +
<pre>
 +
$args = array(
 +
'posts_per_page' => 1,
 +
'post__in'  => get_option('sticky_posts'),
 +
'caller_get_posts' => 1
 +
);
 +
query_posts($args);
 +
</pre>
 +
 
 +
Note: the second method returns only the more recent sticky post; if there are not sticky posts, it returns the last post published.
 +
 
 +
'''To return just the first sticky post or nothing''':
 +
<pre>
 +
$sticky = get_option('sticky_posts');
 +
$args = array(
 +
'posts_per_page' => 1,
 +
'post__in'  => $sticky,
 +
'caller_get_posts' => 1
 +
);
 +
query_posts($args);
 +
if($sticky[0]) {
 +
  // insert here your stuff...
 +
}
 +
</pre>
 +
 
 +
 
 +
'''To exclude all sticky posts from the query''':
 +
query_posts(array("post__not_in" =>get_option("sticky_posts")));
 +
 
 +
'''Return ALL posts with the category, but don't show sticky posts at the top.  The 'sticky posts' will still show in their natural position (e.g. by date):'''
 +
query_posts('caller_get_posts=1&posts_per_page=3&cat=6');
 +
 
 +
'''Return posts with the category, but exclude sticky posts completely, and adhere to paging rules:
 +
<pre>
 +
<?php
 +
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
 +
$sticky=get_option('sticky_posts');
 +
$args=array(
 +
  'cat'=>3,
 +
  'caller_get_posts'=>1,
 +
  'post__not_in' => $sticky,
 +
  'paged'=>$paged,
 +
  );
 +
query_posts($args);
 +
?>
 +
</pre>
 +
 
 +
===Time Parameters <span id="Time_Parameters"></span>===
 +
Retrieve posts belonging to a certain time period.
 +
*  <tt>hour=</tt> - hour (from 0 to 23)
 +
*  <tt>minute=</tt> - minute (from 0 to 60)
 +
*  <tt>second=</tt> - second (0 to 60)
 +
*  <tt>day=</tt> - day of the month (from 1 to 31)
 +
*  <tt>monthnum=</tt> - month number (from 1 to 12)
 +
*  <tt>year=</tt> - 4 digit year (e.g. 2009)
 +
*  <tt>w=</tt> - week of the year (from 0 to 53) and uses the [http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html#function_week MySQL WEEK command Mode=1].
 +
 
 +
'''Returns posts for just the current date:'''
 +
$today = getdate();
 +
query_posts('year=' .$today["year"] .'&monthnum=' .$today["mon"] .'&day=' .$today["mday"] );
 +
 
 +
'''Returns posts dated December 20:'''
 +
query_posts( 'monthnum=12&day=20' );
 +
 
 +
'''Return posts for posts for March 1 to March 15, 2009:'''
 +
<pre>
 +
<?php
 +
//based on Austin Matzko's code from wp-hackers email list
 +
  function filter_where($where = '') {
 +
    //posts for March 1 to March 15, 2009
 +
    $where .= " AND post_date >= '2009-03-01' AND post_date < '2009-03-16'";
 +
    return $where;
 +
  }
 +
add_filter('posts_where', 'filter_where');
 +
query_posts($query_string);
 +
?>
 +
</pre>
 +
 
 +
'''Return posts from the last 30 days:'''
 +
<pre>
 +
<?php
 +
//based on Austin Matzko's code from wp-hackers email list
 +
  function filter_where($where = '') {
 +
    //posts in the last 30 days
 +
    $where .= " AND post_date > '" . date('Y-m-d', strtotime('-30 days')) . "'";
 +
    return $where;
 +
  }
 +
add_filter('posts_where', 'filter_where');
 +
query_posts($query_string);
 +
?>
 +
</pre> 
 +
 
 +
'''Return posts 30 to 60 days old'''
 +
<pre>
 +
<?php
 +
//based on Austin Matzko's code from wp-hackers email list
 +
  function filter_where($where = '') {
 +
    //posts  30 to 60 days old
 +
    $where .= " AND post_date >= '" . date('Y-m-d', strtotime('-60 days')) . "'" . " AND post_date <= '" . date('Y-m-d', strtotime('-30 days')) . "'";
 +
    return $where;
 +
  }
 +
add_filter('posts_where', 'filter_where');
 +
query_posts($query_string);
 +
?>
 +
</pre>
 +
 
 +
===Pagination Parameters <span id="Pagination_Parameters"></span>===
 +
*  <tt>nopaging=true</tt> - will disable pagination, displaying all posts
 +
*  <tt>posts_per_page=10</tt> - number of posts to show per page
 +
*  <tt>paged=2</tt> - show the posts that would normally show up just on page 2 when using the "Older Entries" link. You should set this to <tt>get_query_var( 'paged' )</tt> if you want your query to work with pagination.
 +
*  <tt>order=ASC</tt> - show posts in chronological order, DESC to show in reverse order (the default)
 +
 
 +
===Offset Parameter <span id="Offset_Parameter"></span>===
 +
You can ''displace'' or pass over one or more initial posts which would normally be collected by your query through the use of the offset parameter.
 +
 
 +
The following will display the 5 posts which follow the most recent (1):
 +
 
 +
query_posts('posts_per_page=5&offset=1');
 +
 
 +
===Orderby Parameters <span id="Orderby_Parameters"></span>===
 +
Sort retrieved posts by this field.
 +
*  <tt>orderby=author</tt>
 +
*  <tt>orderby=date</tt>
 +
*  <tt>orderby=title</tt>
 +
*  <tt>orderby=modified</tt>
 +
*  <tt>orderby=menu_order</tt> '''Note:''' Only works with [[Pages]].
 +
*  <tt>orderby=parent</tt>
 +
*  <tt>orderby=ID</tt>
 +
*  <tt>orderby=rand</tt>
 +
*  <tt>orderby=meta_value</tt> '''Note:''' A <tt>meta_key=keyname</tt> must also be present in the query.
 +
*  <tt>orderby=none</tt> - no order (available with [[Version 2.8]])
 +
*  <tt>orderby=comment_count</tt> - (available with [[Version 2.9]])
 +
 
 +
===Order Parameters <span id="Order_Parameters"></span>===
 +
Designates the ascending or descending order of the ORDERBY parameter.
 +
*  <tt>order=ASC</tt> - ascending order, lowest to highest value
 +
*  <tt>order=DESC</tt> - descending order, highest to lowest value
 +
 
 +
===Custom Field Parameters <span id="Custom_Field_Parameters"></span>===
 +
Retrieve posts (or [[Pages]]) based on a custom field key or value. 
 +
 
 +
*  <tt>meta_key=</tt>
 +
*  <tt>meta_value=</tt>
 +
*  <tt>meta_compare=</tt> - operator to test the <tt>meta_value=</tt>, default is '=', with other possible values of '!=', '>', '>=', '<', or '<='
 +
 
 +
Returns posts with custom fields matching both a key of 'color' AND a value of 'blue':
 +
query_posts('meta_key=color&meta_value=blue');
 +
 
 +
Returns posts with a custom field key of 'color', regardless of the custom field value:
 +
query_posts('meta_key=color');
 +
 
 +
Returns posts where the custom field value is 'color', regardless of the custom field key:
 +
query_posts('meta_value=color');
 +
 
 +
Returns any [[Pages|Page]] where the custom field value is 'green', regardless of the custom field key:
 +
query_posts('post_type=page&meta_value=green');
 +
 
 +
Returns both posts and [[Pages]] with a custom field key of 'color' where the custom field value IS NOT EQUAL TO 'blue':
 +
query_posts('post_type=any&meta_key=color&meta_compare=!=&meta_value=blue');
 +
 
 +
Returns posts with custom field key of 'miles' with a custom field value that is LESS THAN OR EQUAL TO 22.  Note the value 99 will be considered greater than 100 as the data is stored as strings, not numbers. 
 +
query_posts('meta_key=miles&meta_compare=<=&meta_value=22');
 +
 
 +
===Combining Parameters <span id="Combining_Parameters"></span>===
 +
You may have noticed from some of the examples above that you combine parameters with an ampersand (&), like so:
 +
 
 +
query_posts('cat=3&year=2004');
 +
 
 +
Posts for category 13, for the current month on the main page:
 +
 
 +
if (is_home()) {
 +
query_posts($query_string . '&cat=13&monthnum=' . date('n',current_time('timestamp')));
 +
}
 +
 
 +
At 2.3 this combination will return posts belong to both Category 1 AND 3, showing just two (2) posts, in descending order by the title:
 +
 
 +
  query_posts(array('category__and'=>array(1,3),'posts_per_page'=>2,'orderby'=>title,'order'=>DESC));
 +
 
 +
 
 +
In 2.3 and 2.5 one would expect the following to return all posts that belong to category 1 and is tagged "apples"
 +
 
 +
query_posts('cat=1&tag=apples');
 +
 
 +
A bug prevents this from happening. See [http://trac.wordpress.org/ticket/5433 Ticket #5433]. A workaround is to search for several tags using +
 +
 
 +
query_posts('cat=1&tag=apples+apples');
 +
 
 +
This will yield the expected results of the previous query. Note that using 'cat=1&tag=apples+oranges' yields expected results.
 +
 
 +
==Examples <span id="Examples"></span>==
 +
 
 +
===Exclude Categories From Your Home Page <span id="Exclude_Categories_From_Your_Home_Page"></span>===
 +
 
 +
Placing this code in your <tt>index.php</tt> file will cause your home page to display posts from all categories ''except'' category ID 3.
 +
 
 +
<pre>
 +
<?php
 +
  if (is_home()) {
 +
      query_posts("cat=-3");
 +
  }
 +
?>
 +
</pre>
 +
 
 +
You can also add some more categories to the exclude-list (tested with WP 2.1.2):
 +
 
 +
<pre>
 +
<?php
 +
  if (is_home()) {
 +
      query_posts("cat=-1,-2,-3");
 +
  }
 +
?>
 +
</pre>
 +
 
 +
===Retrieve a Particular Post <span id="Retrieve_a_Particular_Post"></span>===
 +
To retrieve a particular post, you could use the following:
 +
 
 +
<pre>
 +
<?php
 +
// retrieve one post with an ID of 5
 +
query_posts('p=5');     
 +
?>
 +
</pre>
 +
 
 +
If you want to use the ''Read More'' functionality with this query, you will need to set the global <tt>$more</tt> variable to 0.
 +
 
 +
<pre>
 +
<?php
 +
// retrieve one post with an ID of 5
 +
query_posts('p=5');
 +
     
 +
global $more;
 +
// set $more to 0 in order to only get the first part of the post
 +
$more = 0;
 +
 
 +
// the Loop
 +
while (have_posts()) : the_post();
 +
  // the content of the post
 +
  the_content('Read the full post &raquo;');
 +
endwhile;
 +
?>
 +
</pre>
 +
 
 +
===Retrieve a Particular Page <span id="Retrieve_a_Particular_Page"></span>===
 +
To retrieve a particular page, you could use the following:
 +
 
 +
<pre>
 +
<?php
 +
query_posts('page_id=7');      //retrieves page 7 only
 +
?>
 +
</pre>
 +
or
 +
<pre>
 +
<?php
 +
query_posts('pagename=about'); //retrieves the about page only
 +
?>
 +
</pre>
 +
 
 +
For child pages, the slug of the parent and the child is required, separated by a slash. For example:
 +
 
 +
<pre>
 +
<?php
 +
query_posts('pagename=parent/child'); // retrieve the child page of a parent
 +
?>
 +
</pre>
 +
 
 +
===Passing variables to query_posts <span id="Passing_variables_to_query_posts"></span>===
 +
You can pass a variable to the query with two methods, depending on your needs. As with other examples, place these above your Loop:
 +
 
 +
==== Example 1 <span id=" Example_1"></span>====
 +
 
 +
In this example, we concatenate the query before running it. First assign the variable, then concatenate and then run it. Here we're pulling in a category variable from elsewhere.
 +
 
 +
<pre>
 +
<?php
 +
$categoryvariable=$cat; // assign the variable as current category
 +
$query= 'cat=' . $categoryvariable. '&orderby=date&order=ASC'; // concatenate the query
 +
query_posts($query); // run the query
 +
?>
 +
</pre>
 +
 
 +
==== Example 2 <span id="Example_2"></span>====
 +
 
 +
In this next example, the double quotes tell PHP to treat the enclosed as an expression. For this example, we are getting the current month and the current year, and telling <tt>query_posts</tt> to bring us the posts for the current month/year, and in this case, listing in ascending order so we get the oldest post at the top of the page.
 +
 
 +
<pre>
 +
<?php
 +
$current_month = date('m');
 +
$current_year = date('Y');
 +
 
 +
query_posts("cat=22&year=$current_year&monthnum=$current_month&order=ASC");
 +
?>
 +
<!-- put your loop here -->
 +
</pre>
 +
 
 +
==== Example 3 <span id="Example_3"></span>====
 +
 
 +
This example explains how to generate a complete list of posts, dealing with pagination. We can use the default <tt>$query_string</tt> telling <tt>query_posts</tt> to bring us a full posts listing.  We can also modify the <tt>posts_per_page</tt> query argument from -1 to the number of posts you want to show on each page; in this last case, you'll probably want to use [[テンプレートタグ/posts_nav_link|posts_nav_link()]] to navigate the generated archive.
 +
 
 +
<pre>
 +
<?php
 +
query_posts($query_string.'&posts_per_page=-1');
 +
while(have_posts()) { the_post();
 +
<!-- put your loop here -->
 +
}
 +
?>
 +
</pre>
 +
 
 +
==== Example 4 <span id="Example_4"></span>====
 +
 
 +
If you don't need to use the <tt>$query_string</tt> variable, another method exists that is more clear and readable, in some more complex cases. This method puts the parameters into an array. The same query as in Example 2 above could be done like this:
 +
 
 +
<pre>
 +
query_posts(array(
 +
'cat'      => 22,
 +
'year'    => $current_year,
 +
'monthnum' => $current_month,
 +
'order'    => 'ASC',
 +
));
 +
</pre>
 +
 
 +
As you can see, with this approach, every variable can be put on its own line, for easier reading.
 +
 
 +
=== Preserving the Original Query (Pagination etc.)<span id="Preserving_the_Original_Query_.28Pagination_etc..29"></span>===
 +
By default running query_posts will completely overwrite all existing query variables on the current page. Pagination, categories dates etc. will be lost and only the variables you pass into query_posts will be used.
 +
 
 +
If you want to preserve the original query you can merge the original query array into your parameter array:
 +
 
 +
<pre>
 +
global $wp_query;
 +
query_posts(
 +
array_merge(
 +
array('cat' => 1),
 +
$wp_query->query
 +
)
 +
);</pre>
 +
 
 +
 
 +
==Usage Tips<span id="Usage_Tips"></span>==
 +
The "Blog pages show at most" parameter in Settings > Reading can influence your results.  To overcome this, add the 'posts_per_page' parameter.  For example:
 +
<pre>
 +
query_posts('category_name=The Category Name&posts_per_page=-1');  //returns ALL from the category
 +
</pre>
 +
 
 +
==Resources<span id="Resources"></span>==
 +
* [http://www.ifelse.co.uk/archives/2005/04/08/query_posts-redux/ If..Else - Query Post Redux]
 +
* [http://www.ifelse.co.uk/archives/2005/04/10/make-wp-show-only-one-post-on-the-front-page/ If..Else - Make WordPress Show Only one Post on the Front Page]
 +
* [http://www.ifelse.co.uk/archives/2005/03/31/query_posts/ If..Else - Query Posts]
 +
* [http://perishablepress.com/press/2008/01/22/6-ways-to-customize-wordpress-post-order/ Perishable Press - 6 Ways to Customize WordPress Post Order ]
 +
* [http://www.nietoperzka.com/wptraining/custom-order-of-posts-on-main-page/ nietoperzka's Custom order of posts on the main page]
 +
* [http://www.darrenhoyt.com/2008/06/11/displaying-related-category-and-author-content-in-wordpress/ Displaying related category and author content]
 +
*[http://maymay.net/blog/2008/06/06/arbitrarily-exclude-posts-from-displaying-in-wordpress/ Exclude posts from displaying]
 +
<!--We need resources from other sources for this article-->
 +
 
 +
{{原文|Template Tags/query posts|83483}}<!-- 14:31, 27 February 2010 Scribu 版 -->
 +
 
 +
==Related==
  
 +
{{Query Tags}}
 
{{Query String Tag Footer}}  
 
{{Query String Tag Footer}}  
 
{{PHP Function Tag Footer}}
 
{{PHP Function Tag Footer}}
 +
<!--
 +
{{Tag Footer}}
 +
-->
  
 +
{{DEFAULTSORT:Query posts}}
 
[[Category:テンプレートタグ]]
 
[[Category:テンプレートタグ]]
 
[[Category:wp2.8]]
 
[[Category:wp2.8]]

2010年3月2日 (火) 20:36時点における版

このページ「テンプレートタグ/query posts」は後半未翻訳です。和訳や日本語情報を加筆してくださる協力者を求めています

  • wp_reset_queryや用例などの情報も読めるよう、英語版を後半に挿入しました。前半にない情報を中心に、一部でも翻訳できる方がいたらお願いします。
  • 前半も簡潔で見やすいと思うのですが、どうマージしたらいいでしょう。。
  • 書式: <?php query_posts('arguments'); ?>
  • 説明: ループ内でクエリを変更するために使う。詳しくは、WordPress ループについてのページを参照。
  • 位置: ループ内
  • 引数:
    • 整数が指定できる場合は、特定の投稿を除くためにマイナスの値も使える。たとえば query_posts('cat=-3') とした場合、ID3のカテゴリーのみをクエリから除く。
    • 各引数に複数の値を指定する場合、コンマに続けて追加する。
    • 複数の引数を組み合わせる場合は、他のタグと同じく引数の間に&を挿入する。

カテゴリー引数

指定したIDや名前のカテゴリーに分類された投稿を取得する。

  • cat (整数)- カテゴリーIDを指定。
  • category_name (文字列)- カテゴリー名を指定(日本語も可)。
  • category__and(配列)- カテゴリーIDの配列。指定した複数のカテゴリーすべてに含まれる投稿のみを取得。
  • category__in(配列)- カテゴリーIDの配列。指定した複数のカテゴリーいずれかに含まれる投稿のみを取得。
  • category__not_in(配列)- カテゴリーIDの配列。指定したカテゴリーのいずれにもに含まれない投稿のみを取得。

タグ引数

指定したタグがつけられた投稿を取得する。

  • tag (文字列)- タグスラッグを指定。
  • tag__and(配列)- タグIDの配列。指定した複数のタグすべてを含む投稿のみを取得。
  • tag__in(配列)- タグIDの配列。指定したタグのいずれかを含む投稿のみを取得。
  • tag_slug__and(配列)- タグスラッグの配列。指定した複数のタグすべてを含む投稿のみを取得。
  • tag_slug__in(配列)- タグスラッグの配列。指定したタグのいずれかを含む投稿のみを取得。

投稿者引数

指定したIDや名前の投稿者による投稿を取得する。

  • author (整数)- 投稿者のIDを指定。負の数(-3 など)に指定した場合、その投稿者を除外。
  • author_name (文字列)- 投稿者のニックネームを指定(日本語も可)。

投稿・固定ページ引数

指定したIDや名前の投稿・固定ページを取得する。

  • p (整数)- 投稿ID。
  • name (文字列)- 投稿スラッグ。
  • page_id (整数)- 固定ページID。
  • pagename (文字列)- 固定ページスラッグ。
  • posts_per_page (整数)- 1ページに表示する投稿数。-1 の場合すべてを表示。
  • showposts (整数)- 出力する投稿の数。posts_per_page の導入により非推奨になった。
  • post__in(配列)- array(5,12,2,14,7) のように含める投稿の ID を配列で指定。
  • post__not_in(配列)- array(6,2,8) のように含めない投稿の ID を配列で指定。
  • post_type - page(固定ページ)、post(投稿)、attachment(添付ファイル)、revision(変更履歴)のいずれか。
  • post_status - publish(公開済み)、pending(レビュー待ち)、draft(下書き)、future(スケジュール済み)、private(非公開)、trash(ゴミ箱入り)、inherit(get_children/en 参照)のいずれか。trash の値はバージョン 2.9 以降のみ。
  • post_parent (整数)- 取得したい固定ページの親ページ ID。

先頭固定投稿引数

投稿の先頭固定表示はバージョン 2.7 から導入。

  • array('post__in'=>get_option('sticky_posts')) - すべての先頭固定投稿を配列として返す。
  • caller_get_posts=1 - 返される配列内で、先頭固定投稿を普通の順序で含める(先頭に固定しない)。

日時引数

それぞれ、その日時の間に投稿された投稿を取得する。マイナスの値は使えない。

  • hour (整数)- 0〜23
  • minute (整数)- 0〜60
  • second (整数)- 0〜60
  • day (整数)- 1〜31
  • monthnum (整数)- 1〜12
  • year (整数)- 例: 2009
  • w (整数)- 0〜53 で年間のうちどの週かを選ぶ。MySQL WEEK command Mode=1 を使っている。

ページング引数

  • paged (整数)- インデックスや月別・カテゴリー別アーカイブページが複数ページにわたる場合の、表示するページ数を指定。
  • posts_per_page (整数)- 1ページに出力する投稿数。-1の場合すべての投稿を出力。
  • order (ASC/DESC)- 取得したページの並び順。デフォルト値は「DESC」。

オフセット引数

  • offset (整数)- 取得投稿の先頭からスキップする投稿数を指定。

並び替え引数

カスタムフィールド引数

    • meta_key=
    • meta_value=
    • meta_compare= - meta_value= をテストする演算子。デフォルトは '=' で, '!='、'>'、'>='、'<'、'<=' も使える。

コード記述例

  • 記述例1:

2008年5月に投稿された、カテゴリー ID 1 と 3 の投稿タイトルを1ページに5件ずつ表示。

<?php if (have_posts()) :  query_posts('&posts_per_page=5&cat=1,3&monthnum=5&year=2008');  ?>
  <?php while (have_posts()) : the_post(); ?>
    <h2><?php the_title(); ?></h2>
<?php endwhile; endif; ?>
  • 記述例2:

カテゴリー ID 2 と 6 のいずれにも含まれないすべての投稿のタイトルをリストとして表示。

<?php if (have_posts()) :  query_posts(array('category__not_in' => array(2,6))); ?>
<ul>
 <?php while (have_posts()) : the_post(); ?>
    <li><?php the_title(); ?></li>
<?php endwhile; endif; ?>
</ul>

Description

Query_posts can be used to control which posts show up in The Loop. It accepts a variety of parameters in the same format as used in your URL (e.g. p=4 to show only post of ID number 4).

Why go through all the trouble of changing the query that was meticulously created from your given URL? You can customize the presentation of your blog entries by combining it with page logic (like the Conditional Tags) -- all without changing any of the URLs.

Common uses might be to:

  • Display only a single post on your homepage (a single Page can be done via Settings -> Reading).
  • Show all posts from a particular time period.
  • Show the latest post (only) on the front page.
  • Change how posts are ordered.
  • Show posts from only one category.
  • Exclude one or more categories.

Important note

The query_posts function is intended to be used to modify the main page Loop only. It is not intended as a means to create secondary Loops on the page. If you want to create separate Loops outside of the main one, you should create separate WP_Query objects and use those instead. Use of query_posts on Loops other than the main one can result in your main Loop becoming incorrect and possibly displaying things that you were not expecting.

The query_posts function overrides and replaces the main query for the page. To save your sanity, do not use it for any other purpose.

Usage Usage

<?php

//The Query
query_posts('posts_per_page=5');

//The Loop
if ( have_posts() ) : while ( have_posts() ) : the_post();
 ..
endwhile; else:
 ..
endif;

//Reset Query
wp_reset_query();

?>

Usage Note

Place a call to query_posts() in one of your Template files before The Loop begins. The wp_query object will generate a new SQL query using your parameters. When you do this, WordPress ignores the other parameters it receives via the URL (such as page number or category). If you want to preserve that information, you can use the $query_string global variable in the call to query_posts().

For example, to set the display order of the posts without affecting the rest of the query string, you could place the following before The Loop:

global $query_string;
query_posts($query_string . "&order=ASC");

When using query_posts in this way, the quoted portion of the argument must begin with an ampersand (&).


Parameters

This is not an exhaustive list yet. It is meant to show some of the more common things possible with setting your own queries.

Category Parameters

Show posts only belonging to certain categories.

  • cat - must use cat ids
  • category_name
  • category__and - must use cat ids
  • category__in - must use cat ids
  • category__not_in - must use cat ids

Show One Category by ID

Display posts from only one category ID (and any children of that category):

query_posts('cat=4');

Show One Category by Name

Display posts from only one category by name:

query_posts('category_name=Staff Home');

Show Several Categories by ID

Display posts from several specific category IDs:

query_posts('cat=2,6,17,38');

Exclude Posts Belonging to Only One Category

Show all posts except those from a category by prefixing its ID with a '-' (minus) sign.

query_posts('cat=-3');

This excludes any post that belongs to category 3.

Multiple Category Handling

Display posts that are in multiple categories. This shows posts that are in both categories 2 and 6:

query_posts(array('category__and' => array(2,6)));

To display posts from either category 2 OR 6, you could use cat as mentioned above, or by using category__in (note this does not show posts from any children of these categories):

query_posts(array('category__in' => array(2,6)));

You can also exclude multiple categories this way:

query_posts(array('category__not_in' => array(2,6)));

Tag Parameters

Show posts associated with certain tags.

  • tag
  • tag_id - must use tag ids
  • tag__and - must use tag ids
  • tag__in - must use tag ids
  • tag__not_in - must use tag ids
  • tag_slug__and
  • tag_slug__in

Fetch posts for one tag

query_posts('tag=cooking');

Fetch posts that have either of these tags

query_posts('tag=bread,baking');

Fetch posts that have all three of these tags:

query_posts('tag=bread+baking+recipe');

Multiple Tag Handling

Display posts that are tagged with both tag id 37 and tag id 47:

query_posts(array('tag__and' => array(37,47));

To display posts from either tag id 37 or 47, you could use tag as mentioned above, or explicitly specify by using tag__in:

query_posts(array('tag__in' => array(37,47));

Display posts that do not have any of the two tag ids 37 and 47:

query_posts(array('tag__not_in' => array(37,47));

The tag_slug__in and tag_slug__and behave much the same, except match against the tag's slug.

Also see Ryan's discussion of Tag intersections and unions.

Author Parameters

You can also restrict the posts by author.

  • author=3
  • author=-3 - exclude author id 3 posts
  • author_name=Harriet

Note: author_name operates on the user_nicename field, whilst author operates on the author id field.

Display all Pages for author=1, in title order, with no sticky posts tacked to the top:

query_posts('caller_get_posts=1&author=1&post_type=page&post_status=publish&orderby=title&order=ASC');

Post & Page Parameters

Retrieve a single post or page.

  • 'p' => 27 - use the post ID to show that post
  • 'name' => 'about-my-life' - query for a particular post that has this Post Slug
  • 'page_id' => 7 - query for just Page ID 7
  • 'pagename' => 'about' - note that this is not the page's title, but the page's path
  • 'posts_per_page' => 1 - use 'posts_per_page' => 3 to show 3 posts. Use 'posts_per_page' => -1 to show all posts
  • 'showposts' => 1 - use 'showposts' => 3 to show 3 posts. Use 'showposts' => -1 to show all posts. Deprecated in favor of posts_per_page
  • 'post__in' => array(5,12,2,14,7) - inclusion, lets you specify the post IDs to retrieve
  • 'post__not_in' => array(6,2,8) - exclusion, lets you specify the post IDs NOT to retrieve
  • 'post_type' => 'page' - returns Pages; defaults to value of post; can be any, attachment, page, post, or revision. any retrieves any type except revisions.
  • 'post_status' => 'publish' - returns publish works. Also could use pending, draft, future, private, trash. For inherit see get_children/en. Status of trash added with Version 2.9.
  • 'post_parent' => 93 - return just the child Pages of Page 93.

Sticky Post Parameters

Sticky posts first became available with WordPress Version 2.7. Posts that are set as Sticky will be displayed before other posts in a query, unless excluded with the caller_get_posts=1 parameter.

  • array('post__in'=>get_option('sticky_posts')) - returns array of all sticky posts
  • caller_get_posts=1 - To exclude sticky posts being included at the beginning of posts returned, but the sticky post will still be returned in the natural order of that list of posts returned.

To return just the first sticky post:

$sticky=get_option('sticky_posts') ; 
query_posts('p=' . $sticky[0]);

or

$args = array(
	'posts_per_page' => 1,
	'post__in'  => get_option('sticky_posts'),
	'caller_get_posts' => 1
);
query_posts($args);

Note: the second method returns only the more recent sticky post; if there are not sticky posts, it returns the last post published.

To return just the first sticky post or nothing:

$sticky = get_option('sticky_posts');
$args = array(
	'posts_per_page' => 1,
	'post__in'  => $sticky,
	'caller_get_posts' => 1
);
query_posts($args);
if($sticky[0]) {
   // insert here your stuff...
}


To exclude all sticky posts from the query:

query_posts(array("post__not_in" =>get_option("sticky_posts")));

Return ALL posts with the category, but don't show sticky posts at the top. The 'sticky posts' will still show in their natural position (e.g. by date):

query_posts('caller_get_posts=1&posts_per_page=3&cat=6'); 

Return posts with the category, but exclude sticky posts completely, and adhere to paging rules:

<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$sticky=get_option('sticky_posts');
$args=array(
   'cat'=>3,
   'caller_get_posts'=>1,
   'post__not_in' => $sticky,
   'paged'=>$paged,
   );
query_posts($args);
?>

Time Parameters

Retrieve posts belonging to a certain time period.

  • hour= - hour (from 0 to 23)
  • minute= - minute (from 0 to 60)
  • second= - second (0 to 60)
  • day= - day of the month (from 1 to 31)
  • monthnum= - month number (from 1 to 12)
  • year= - 4 digit year (e.g. 2009)
  • w= - week of the year (from 0 to 53) and uses the MySQL WEEK command Mode=1.

Returns posts for just the current date:

$today = getdate();
query_posts('year=' .$today["year"] .'&monthnum=' .$today["mon"] .'&day=' .$today["mday"] );

Returns posts dated December 20:

query_posts( 'monthnum=12&day=20' );

Return posts for posts for March 1 to March 15, 2009:

<?php
//based on Austin Matzko's code from wp-hackers email list
  function filter_where($where = '') {
    //posts for March 1 to March 15, 2009
    $where .= " AND post_date >= '2009-03-01' AND post_date < '2009-03-16'";
    return $where;
  }
add_filter('posts_where', 'filter_where');
query_posts($query_string);
?>

Return posts from the last 30 days:

<?php
//based on Austin Matzko's code from wp-hackers email list
  function filter_where($where = '') {
    //posts in the last 30 days
    $where .= " AND post_date > '" . date('Y-m-d', strtotime('-30 days')) . "'";
    return $where;
  }
add_filter('posts_where', 'filter_where');
query_posts($query_string);
?>

Return posts 30 to 60 days old

<?php
//based on Austin Matzko's code from wp-hackers email list
  function filter_where($where = '') {
    //posts  30 to 60 days old
    $where .= " AND post_date >= '" . date('Y-m-d', strtotime('-60 days')) . "'" . " AND post_date <= '" . date('Y-m-d', strtotime('-30 days')) . "'";
    return $where;
  }
add_filter('posts_where', 'filter_where');
query_posts($query_string);
?>

Pagination Parameters

  • nopaging=true - will disable pagination, displaying all posts
  • posts_per_page=10 - number of posts to show per page
  • paged=2 - show the posts that would normally show up just on page 2 when using the "Older Entries" link. You should set this to get_query_var( 'paged' ) if you want your query to work with pagination.
  • order=ASC - show posts in chronological order, DESC to show in reverse order (the default)

Offset Parameter

You can displace or pass over one or more initial posts which would normally be collected by your query through the use of the offset parameter.

The following will display the 5 posts which follow the most recent (1):

query_posts('posts_per_page=5&offset=1');

Orderby Parameters

Sort retrieved posts by this field.

  • orderby=author
  • orderby=date
  • orderby=title
  • orderby=modified
  • orderby=menu_order Note: Only works with Pages.
  • orderby=parent
  • orderby=ID
  • orderby=rand
  • orderby=meta_value Note: A meta_key=keyname must also be present in the query.
  • orderby=none - no order (available with Version 2.8)
  • orderby=comment_count - (available with Version 2.9)

Order Parameters

Designates the ascending or descending order of the ORDERBY parameter.

  • order=ASC - ascending order, lowest to highest value
  • order=DESC - descending order, highest to lowest value

Custom Field Parameters

Retrieve posts (or Pages) based on a custom field key or value.

  • meta_key=
  • meta_value=
  • meta_compare= - operator to test the meta_value=, default is '=', with other possible values of '!=', '>', '>=', '<', or '<='

Returns posts with custom fields matching both a key of 'color' AND a value of 'blue':

query_posts('meta_key=color&meta_value=blue'); 

Returns posts with a custom field key of 'color', regardless of the custom field value:

query_posts('meta_key=color'); 

Returns posts where the custom field value is 'color', regardless of the custom field key:

query_posts('meta_value=color');

Returns any Page where the custom field value is 'green', regardless of the custom field key:

query_posts('post_type=page&meta_value=green');

Returns both posts and Pages with a custom field key of 'color' where the custom field value IS NOT EQUAL TO 'blue':

query_posts('post_type=any&meta_key=color&meta_compare=!=&meta_value=blue');

Returns posts with custom field key of 'miles' with a custom field value that is LESS THAN OR EQUAL TO 22. Note the value 99 will be considered greater than 100 as the data is stored as strings, not numbers.

query_posts('meta_key=miles&meta_compare=<=&meta_value=22');

Combining Parameters

You may have noticed from some of the examples above that you combine parameters with an ampersand (&), like so:

query_posts('cat=3&year=2004');

Posts for category 13, for the current month on the main page:

if (is_home()) {
query_posts($query_string . '&cat=13&monthnum=' . date('n',current_time('timestamp')));
}

At 2.3 this combination will return posts belong to both Category 1 AND 3, showing just two (2) posts, in descending order by the title:

 query_posts(array('category__and'=>array(1,3),'posts_per_page'=>2,'orderby'=>title,'order'=>DESC));


In 2.3 and 2.5 one would expect the following to return all posts that belong to category 1 and is tagged "apples"

query_posts('cat=1&tag=apples');

A bug prevents this from happening. See Ticket #5433. A workaround is to search for several tags using +

query_posts('cat=1&tag=apples+apples');

This will yield the expected results of the previous query. Note that using 'cat=1&tag=apples+oranges' yields expected results.

Examples

Exclude Categories From Your Home Page

Placing this code in your index.php file will cause your home page to display posts from all categories except category ID 3.

<?php
   if (is_home()) {
      query_posts("cat=-3");
   }
?>

You can also add some more categories to the exclude-list (tested with WP 2.1.2):

<?php
   if (is_home()) {
      query_posts("cat=-1,-2,-3");
   }
?>

Retrieve a Particular Post

To retrieve a particular post, you could use the following:

<?php
// retrieve one post with an ID of 5
query_posts('p=5');      
?>

If you want to use the Read More functionality with this query, you will need to set the global $more variable to 0.

<?php
// retrieve one post with an ID of 5
query_posts('p=5');
      
global $more;
// set $more to 0 in order to only get the first part of the post
$more = 0; 

// the Loop
while (have_posts()) : the_post(); 
  // the content of the post
  the_content('Read the full post »'); 
endwhile;
?>

Retrieve a Particular Page

To retrieve a particular page, you could use the following:

<?php
query_posts('page_id=7');      //retrieves page 7 only
?>

or

<?php
query_posts('pagename=about'); //retrieves the about page only
?>

For child pages, the slug of the parent and the child is required, separated by a slash. For example:

<?php
query_posts('pagename=parent/child'); // retrieve the child page of a parent
?>

Passing variables to query_posts

You can pass a variable to the query with two methods, depending on your needs. As with other examples, place these above your Loop:

Example 1

In this example, we concatenate the query before running it. First assign the variable, then concatenate and then run it. Here we're pulling in a category variable from elsewhere.

 <?php
 $categoryvariable=$cat; // assign the variable as current category
 $query= 'cat=' . $categoryvariable. '&orderby=date&order=ASC'; // concatenate the query
 query_posts($query); // run the query
 ?>

Example 2

In this next example, the double quotes tell PHP to treat the enclosed as an expression. For this example, we are getting the current month and the current year, and telling query_posts to bring us the posts for the current month/year, and in this case, listing in ascending order so we get the oldest post at the top of the page.

<?php
$current_month = date('m');
$current_year = date('Y');

query_posts("cat=22&year=$current_year&monthnum=$current_month&order=ASC");
?>
<!-- put your loop here -->

Example 3

This example explains how to generate a complete list of posts, dealing with pagination. We can use the default $query_string telling query_posts to bring us a full posts listing. We can also modify the posts_per_page query argument from -1 to the number of posts you want to show on each page; in this last case, you'll probably want to use posts_nav_link() to navigate the generated archive.

<?php 
query_posts($query_string.'&posts_per_page=-1');
while(have_posts()) { the_post();
<!-- put your loop here -->
}
?>

Example 4

If you don't need to use the $query_string variable, another method exists that is more clear and readable, in some more complex cases. This method puts the parameters into an array. The same query as in Example 2 above could be done like this:

query_posts(array(
 'cat'      => 22, 
 'year'     => $current_year, 
 'monthnum' => $current_month, 
 'order'    => 'ASC',
));

As you can see, with this approach, every variable can be put on its own line, for easier reading.

Preserving the Original Query (Pagination etc.)

By default running query_posts will completely overwrite all existing query variables on the current page. Pagination, categories dates etc. will be lost and only the variables you pass into query_posts will be used.

If you want to preserve the original query you can merge the original query array into your parameter array:

global $wp_query;
query_posts(
	array_merge(
		array('cat' => 1),
		$wp_query->query
	)
);


Usage Tips

The "Blog pages show at most" parameter in Settings > Reading can influence your results. To overcome this, add the 'posts_per_page' parameter. For example:

query_posts('category_name=The Category Name&posts_per_page=-1');  //returns ALL from the category

Resources

最新英語版: WordPress Codex » Template Tags/query posts最新版との差分

Related

記事

コード・ドキュメンテーション

  • クラス: WP_Query - WP_Query クラスの詳細な全容
  • クラス: WP_Comment_Query - コメント関連のクエリのためのクラス
  • クラス: WP_User_Query - ユーザー関連のクエリのためのクラス
  • オブジェクト: $wpdb - $wpdb オブジェクトの使い方全容
  • 関数: set_query_var()
  • 関数: get_query_var()
  • 関数: query_posts() - 追加のカスタムクエリを作成
  • 関数: get_post() - 項目の ID を取得しデータベース内にあるその投稿のレコードを返す
  • 関数: get_posts() - 投稿の配列を返すことに特化した関数
  • 関数: get_pages() - ページの配列を返すことに特化した関数
  • 関数: have posts() - クエリが投稿を返すか否かを判断する条件関数
  • 関数: the_post() - クエリ後に自動的にループを設定する
  • 関数: rewind_posts() - 現状のループをリセットする
  • 関数: setup_postdata() - ループ内で個別の結果を得るためのクエリデータを設定する
  • 関数: wp_reset_postdata() - 直前のクエリを復元する (通常はループ内の別のループの後に用いられる)
  • 関数: wp_reset_query()
  • 関数: is_main_query() - 変更されるクエリがメインのクエリであることを確認する
  • アクションフック: pre_get_posts - WordPressクエリが実行される前に変更する
  • アクションフック: the_post - post クエリの後で post オブジェクトを変更する
  • フィルターフック: found_posts - WP_Query オブジェクトの found_posts 値を変更する


テンプレートタグへのクエリ文字列型パラメータの渡し方関数リファレンステンプレートタグ目次もご覧ください。


テンプレートタグへの PHP 関数型パラメータの渡し方関数リファレンステンプレートタグ目次もご覧ください。