P2 Resolved Posts: Only mark a specific category as unresolved

In the WordPress.org forums, ameeromar asks:

Hey would it be possible to limit the automatically marking as unresolved to one category? This would be particularly useful for my category ‘tasks’ which need to be marked as unresolved when published (and then marked as resolved when completed).

It’s totally possible using a combination of a couple filters. Here’s what the annotated code snippet looks like:

[code lang=”php”]
/**
* P2 Resolved Posts: Only mark ‘task’ posts as unresolved
*
* @see http://danielbachhuber.com/2013/04/18/p2-resolved-posts-only-mark-a-specific-category-as-unresolved/
*/
// Marks all new posts as unresolved
add_filter( ‘p2_resolved_posts_mark_new_as_unresolved’, ‘__return_true’ );
// Let us apply conditional logic to when posts are marked unresolved
add_filter( ‘p2_resolved_posts_maybe_mark_new_as_unresolved’, ‘p2rpx_only_mark_tasks’, 10, 2 );
function p2rpx_only_mark_tasks( $ret, $post ) {

// Get all of the categories assigned to the post
$cats = get_the_terms( $post->ID, ‘category’ );
// Make sure this didn’t return false or a WP_Error object
if ( is_array( $cats ) ) {

// Use wp_filter_object_list() to see if there are any ‘task’ terms
$task = wp_filter_object_list( $cats, array( ‘slug’ => ‘task’ ) );
// If there is a task term, we want to mark unresolved. Otherwise, no.
if ( ! empty( $task ) )
$ret = true;
else
$ret = false;
}

return $ret;
}
[/code]

However, in a stock P2 install, there isn’t a frontend interface for setting the category. The category is determined by the post format you use. Other users might be better off searching for tags by switching the term lookup to: get_the_terms( $post->ID, 'post_tag' );

Join the Conversation

1 Comment

Leave a comment

Your email address will not be published. Required fields are marked *