Add post formats to categorized posts

This here site predates post formats in core, so many of my older posts are categorized with a post format but missing an actual post format. Now that I’ve switched to the lovely TwentyThirteen, I decided to fix things up.

Here’s the bit of PHP I wrote:

[sourcecode language=”php”]
global $wpdb;
$post_ids = $wpdb->get_col( "SELECT ID FROM $wpdb->posts WHERE post_type=’post’ AND post_status=’publish’;" );

$category_to_format = array(
‘asides’ => ‘aside’,
‘galleries’ => ‘gallery’,
‘photos’ => ‘photo’,
‘quotes’ => ‘quote’,
‘statuses’ => ‘status’,
‘videos’ => ‘video’
);

foreach( $post_ids as $post_id ) {
$category = array_shift( get_the_terms( $post_id, ‘category’ ) );
if ( ! get_post_format( $post_id ) && array_key_exists( $category->slug, $category_to_format ) ) {
set_post_format( $post_id, $category_to_format[$category->slug] );
echo "Changed {$post_id} to {$category_to_format[$category->slug]}n";
}
}
[/sourcecode]

wp-cli offers a quick and dirty way to execute the PHP without even creating a file:

[sourcecode language=”bash”]
wp> global $wpdb;
wp> $post_ids = $wpdb->get_col( "SELECT ID FROM $wpdb->posts WHERE post_type=’publish’ AND post_status=’publish’;" );
wp> $category_to_format = array( ‘asides’ => ‘aside’,’galleries’ => ‘gallery’,’photos’ => ‘photo’,’quotes’ => ‘quote’,’statuses’ => ‘status’,’videos’ => ‘video’ );
wp> foreach( $post_ids as $post_id ) { $category = array_shift( get_the_terms( $post_id, ‘category’ ) ); if ( ! get_post_format( $post_id ) && array_key_exists( $category->slug, $category_to_format ) ) { set_post_format( $post_id, $category_to_format[$category->slug] ); echo "Changed {$post_id} to {$category_to_format[$category->slug]}n"; } }
[/sourcecode]

Et, voila! Because I haven’t used categories for anything more than post formats, I’m also hiding them from the admin and automagically setting based on post format.

Ten points to whomever adds multiline input to wp shell.

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' );

Include posts by matching authors in your search results

Out of the box, WordPress’ search isn’t that great. It only returns posts based on a LIKE query against the post title or post content. Often, you’ll want it to match against other data associated with your post, possibly including the author, tags, post meta fields, etc. These queries can get complex to perform on the fly, however.

The following code snippet allows you to include posts by matching authors in your search results. We’re modifying any search queries to also include all posts whose author display name or user login matches the query. You can change this to be the author’s first name, last name, or other fields.

Updated May 17, 2012: Improved the user search so we don’t query for all users and search with PHP; instead, search against the users table.

Allowing authors to set co-authors

In the WordPress.org forums, whoaloic asks:

I have a site with multiple authors.
I would like to allow authors who create a post or a page to give rights to other authors.
For now, only administrator and editor can do that.
Is there a solution?

Yep, totally doable. By default, Co-Authors Plus defaults to ‘edit_others_posts’ as the required cap for changing co-authors. With the following code snippet in your theme’s functions.php file, you can make that cap ‘publish_posts’ instead (which authors and above usually have).

[sourcecode language=”php”]
/**
* Filter the Co-Authors Plus current_user_can_set_authors() function
* so that users with ‘publish_posts’ can set Co-Authors
*
* @author danielbachhuber
*
* @see https://github.com/danielbachhuber/Co-Authors-Plus/issues/8
* @see http://wordpress.org/support/topic/plugin-co-authors-plus-allow-authors-post-to-give-access-to-other-authors
*/
add_filter( ‘coauthors_plus_edit_authors’, ‘db_filter_coauthors_edit_cap’ );
function db_filter_coauthors_edit_cap( $cap_result ) {
global $coauthors_plus;

$post_type = $coauthors_plus->get_current_post_type();
if( ! $post_type ) return false;

$post_type_object = get_post_type_object( $post_type );
return current_user_can( $post_type_object->cap->publish_posts );
}
[/sourcecode]

In preparing this snippet, I also opened a couple of issues in Github:

Show biographies for co-authors at the end of your post

In the WordPress.org forums, doubleedesign says:

I want to add the authors’ biographies to the end of each post.

Awesome… it’s pretty simple to do. Conceptually, what we need to do is load our co-authors, and then loop through printing the relevant information for each one.

You’ll want to put the following code snippet within The Loop in any template you’d like the bios to appear.

[sourcecode language=”php”]
/**
* Show multiple Co-Author biography fields at the bottom of a single post
* This snippet should be placed within The Loop
*/
if ( class_exists( ‘coauthors_plus’ ) ) {
// Get the Co-Authors for the post
$co_authors = get_coauthors();
// For each Co-Author, echo a wrapper div, their name, and their bio if they have one
foreach ( $co_authors as $key => $co_author ) {
$co_author_classes = array(
‘co-author-wrap’,
‘co-author-number-‘ . ( $key + 1 ),
);
echo ‘<div class="’ . implode( ‘ ‘, $co_author_classes ) . ‘">’;
echo ‘<h4 class="co-author-display-name">’ . $co_author->display_name . ‘</h4>’;
// Only print the description if the description exists
if ( $description = get_the_author_meta( ‘description’, $co_author->ID ) )
echo ‘<p class="co-author-bio">’ . $description . ‘</p>’;
echo ‘</div>’;
}
}
[/sourcecode]

If you’d like other co-author details to appear as well, like their avatar for instance, you can modify the output within the foreach loop.

Scripting my application launch process

Every day for work, there’s several applications I always use. The other day, I put together a quick and dirty bash script for opening all of them at once.

I’m terribly inexperienced at this, so don’t poke fun, only offer good suggestions for improvement…

[sourcecode language=”bash”]
#!/bin/bash

# Open all of the requisite applications
echo ‘Opening Chrome’
`open /Volumes/Macintosh HD/Applications/Google Chrome.app/`
echo ‘Opening Skype’
`open /Volumes/Macintosh HD/Applications/Skype.app/`
echo ‘Opening Sparrow’
`open /Volumes/Macintosh HD/Applications/Sparrow.app/`
echo ‘Opening Linkus’
`open /Volumes/Macintosh HD/Applications/Linkinus.app/`
echo ‘Opening Adium’
`open /Volumes/Macintosh HD/Applications/Adium.app/`
[/sourcecode]

Show matching terms in your search results

WordPress’ internal search isn’t all that great out of the box, as I’ve discussed before. For instance, tags and categories aren’t included in the search query; as such, if your post is tagged “apple”, but there is no mention of “apple” in the title or post content, the post won’t be included in your search results.

One partial workaround is to search taxonomy terms against your query and include those in your results. I did this previously with the CUNY J-School’s tech website:

You can have something similar with the code snippet below.

[sourcecode lang=”php”]<?php
/**
* Show matching terms for a given search query
* Best placed under the_search_form() in search.php
*/
global $wp_query;
// Only show the matching terms on the first page of results
if ( $wp_query->query_vars[‘paged’] <= 1 ) {
$args = array(
‘search’ => get_search_query(),
‘orderby’ => ‘none’,
);
// You can change the first argument to an array of whatever taxonomies you want to search against
$matching_terms = get_terms( array( ‘post_tag’, ‘category’ ), $args );
if ( count( $matching_terms ) ) {
echo ‘<div class="all-matching-terms">Looking for? ‘;
$all_terms = ”;
foreach ( $matching_terms as $matching_term ) {
$all_terms .= ‘<a ‘;
if ( $matching_term->description )
$all_terms .= ‘title="’ . esc_attr( $matching_term->description ) . ‘" ‘;
$all_terms .= ‘href="’ . esc_url( get_term_link( $matching_term, $matching_term->taxonomy ) ) . ‘">’ . esc_html( $matching_term->name ) . ‘</a>, ‘;
}
echo rtrim( $all_terms, ‘, ‘ );
echo ‘</div>’;
}
}[/sourcecode]

Co-authors in your RSS feeds

In the WordPress.org forums, razorfrog asks:

Short of editing core WordPress code, is it possible to display the multiple authors in the site’s RSS feed?

Of course there is! From the source code, we know the RSS feed template uses the_author() to display the post’s byline information. Furthermore, the_author() echoes get_the_author() which is a filterable function. Filters allow us to programmatically change values used in a function. What we need to do is write a short snippet to produce the co-authors byline when an RSS feed is requested and Co-Authors Plus is activated.

The snippet is as follows, and can be placed in your theme’s functions.php file or a standalone MU plugin.

[sourcecode lang=”php”]/**
* Co-authors in RSS and other feeds
* /wp-includes/feed-rss2.php uses the_author(), so we selectively filter the_author value
*/
function db_coauthors_in_rss( $the_author ) {

if ( !is_feed() || !function_exists( ‘coauthors’ ) )
return $the_author;

return coauthors( null, null, null, null, false );
}
add_filter( ‘the_author’, ‘db_coauthors_in_rss’ );[/sourcecode]