Created
September 25, 2016 19:51
-
-
Save hellofromtonya/2166c0122b6a879461f9435dc99ed493 to your computer and use it in GitHub Desktop.
Limits the adjacent post to just parents (no children)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Get the next adjacent parent post. | |
* | |
* This function extends the SQL WHERE query of the WordPress get_adjacent_post() | |
* function. It registers a callback to the `get_next_post_where` event filter, | |
* which then adds a new WHERE parameter. | |
* | |
* @uses get_next_post() | |
* @uses `get_next_post_where` filter | |
* @uses fulcrum_add_parent_post_to_adjacent_sql() | |
* | |
* @since 1.0.6 | |
* | |
* @param bool $in_same_term Optional. Whether post should be in a same taxonomy term. Default false. | |
* @param array|string $excluded_terms Optional. Array or comma-separated list of excluded term IDs. Default empty. | |
* @param string $taxonomy Optional. Taxonomy, if $in_same_term is true. Default 'category'. | |
* | |
* @return null|string|WP_Post Post object if successful. Null if global $post is not set. Empty string if no | |
* corresponding post exists. | |
*/ | |
function fulcrum_get_next_parent_post( $in_same_term = false, $excluded_terms = '', $taxonomy = 'category') { | |
add_filter( 'get_next_post_where', 'fulcrum_add_parent_post_to_adjacent_sql' ); | |
return get_next_post( $in_same_term, $excluded_terms, $taxonomy ); | |
} | |
/** | |
* Get the previous adjacent parent post. | |
* | |
* This function extends the SQL WHERE query of the WordPress get_adjacent_post() | |
* function. It registers a callback to the `get_previous_post_where` event filter, | |
* which then adds a new WHERE parameter. | |
* | |
* @uses get_previous_post() | |
* @uses `get_previous_post_where` filter | |
* @uses fulcrum_add_parent_post_to_adjacent_sql() | |
* | |
* @since 1.0.6 | |
* | |
* @param bool $in_same_term Optional. Whether post should be in a same taxonomy term. Default false. | |
* @param array|string $excluded_terms Optional. Array or comma-separated list of excluded term IDs. Default empty. | |
* @param string $taxonomy Optional. Taxonomy, if $in_same_term is true. Default 'category'. | |
* | |
* @return null|string|WP_Post Post object if successful. Null if global $post is not set. Empty string if no | |
* corresponding post exists. | |
*/ | |
function fulcrum_get_previous_parent_post( $in_same_term = false, $excluded_terms = '', $taxonomy = 'category') { | |
add_filter( 'get_previous_post_where', 'fulcrum_add_parent_post_to_adjacent_sql' ); | |
return get_previous_post( $in_same_term, $excluded_terms, $taxonomy ); | |
} | |
/** | |
* Adds a post parent WHERE SQL check to the adjacent SQL. | |
* | |
* In WordPress, the column `post_parent` is 0 when the content is | |
* the root parent. | |
* | |
* Callback for the WordPress filter events `get_previous_post_where` and | |
* `get_next_post_where`. | |
* | |
* @since 1.0.6 | |
* | |
* @param string $where_sql | |
* | |
* @return string | |
*/ | |
function fulcrum_add_parent_post_to_adjacent_sql( $where_sql ) { | |
$where_sql .= ' AND p.post_parent = 0'; | |
return $where_sql; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks, this is really helpful!