Skip to content

Instantly share code, notes, and snippets.

@chrisguitarguy
Created December 20, 2011 21:09
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chrisguitarguy/1503285 to your computer and use it in GitHub Desktop.
Save chrisguitarguy/1503285 to your computer and use it in GitHub Desktop.
An example of how to use the posts_where filter in WordPress: don't allow any top-level parents posts into a query.
<?php
add_filter( 'posts_where', 'wpse29897_no_parents', 10, 2 );
function wpse29897_no_parents( $where, $query )
{
if( isset( $query->query_vars['post_type'] ) && 'city' == $query->query_vars['post_type'] )
{
if( '' != $where )
{
$where .= ' AND post_parent != 0';
}
else
{
$where .= ' post_parent != 0';
}
}
return $where;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment