pre_get_posts
which change the query vars just before the main query is executed.
Here is an example from the codex to exclude categories from the home page/blog page. For a full list of available parameters and values that you can use with pre_get_posts
, visit the WP_Query
codex page:
[php]function exclude_category( $query ) {
if ( $query->is_home() && $query->is_main_query() ) {
$query->set( 'cat', '-1,-1347' );
}
}
add_action( 'pre_get_posts', 'exclude_category' );[/php]
Revisions
- June 3, 2015 @ 06:18:43 [Current Revision] by PeterLugg
- June 3, 2015 @ 06:18:43 by PeterLugg
- June 3, 2015 @ 06:18:13 by PeterLugg
Revision Differences
June 3, 2015 @ 06:18:13 | Current Revision | ||
---|---|---|---|
Content | |||
Unchanged: The correct way to do this is to make use of <a href="http:// codex.wordpress.org/Plugin_ API/Action_Reference/ pre_get_posts" rel="nofollow" ><code>pre_get_ posts</code></a> which change the query vars just before the main query is executed. | Unchanged: The correct way to do this is to make use of <a href="http:// codex.wordpress.org/Plugin_ API/Action_Reference/ pre_get_posts" rel="nofollow" ><code>pre_get_ posts</code></a> which change the query vars just before the main query is executed. | ||
Unchanged: Here is an example from the codex to exclude categories from the home page/blog page. For a full list of available parameters and values that you can use with <code>pre_get_ posts</code>, visit the <a href="http:// codex.wordpress.org/Class_ Reference/WP_Query" rel="nofollow" ><code>WP_Query</code></a> codex page: | Unchanged: Here is an example from the codex to exclude categories from the home page/blog page. For a full list of available parameters and values that you can use with <code>pre_get_ posts</code>, visit the <a href="http:// codex.wordpress.org/Class_ Reference/WP_Query" rel="nofollow" ><code>WP_Query</code></a> codex page: | ||
Deleted: [php]</pre> | |||
Added: [php]function exclude_category( $query ) { | |||
Deleted: <pre class="default prettyprint prettyprinted" ><code><span class="kwd" >function< /span><span class="pln"> exclude_category< /span><span class="pun" >(</span><span class="pln"> $query </span><span class="pun" >)</span> <span class="pun" >{</span> | Added: if ( $query->is_home() && $query->is_ main_query() ) { | ||
Deleted: <span class="kwd" >if</span> <span class="pun" >(</span><span class="pln"> $query</span><span class="pun" >->< /span><span class="pln" >is_home< /span><span class="pun" >()</span> <span class="pun" >&& </span><span class="pln"> $query</span><span class="pun" >->< /span><span class="pln" >is_main_query< /span><span class="pun" >()</span> <span class="pun" >)</span> <span class="pun" >{</span><span class="pln"> | |||
Deleted: $query</span><span class="pun" >->< /span><span class="kwd" >set</span><span class="pun" >(</span> <span class="str" >'cat'< /span><span class="pun" >,</span> <span class="str" >'-1,-1347'</span> <span class="pun" >);</span> | Added: $query->set( 'cat', '-1,-1347' ); | ||
Deleted: <span class="pun" >}</span> | Added: } | ||
Deleted: <span class="pun" >}</span><span class="pln"> | |||
Added: } | |||
Deleted: add_action< /span><span class="pun" >(</span> <span class="str" >'pre_get_ posts'</span><span class="pun" >,</span> <span class="str" >'exclude_ category'</span> <span class="pun" >);</span> </code> </pre> | Added: add_action( 'pre_get_posts', 'exclude_category' );[/php] | ||
Deleted: <pre>[/php] |
Note: Spaces may be added to comparison text to allow better line wrapping.
No comments yet.