| Task | Code |
|---|---|
| WordPress Get Related Posts by Tag/Taxonomy Term in WordPress » | $taxonomy = 'post_tags'; // or whatever you want
$query = NULL;
$terms = wp_get_post_terms($post->ID, $taxonomy);
if ($terms) {
// Get the term ids into an array for the query args
$term_ids = array();
foreach($terms as $individual_term) $term_ids[] = $individual_term->term_id;
$args=array(
'post_type' => get_post_type(),
'tax_query' => array(
array(
'taxonomy' => $taxonomy,
'field' => 'id',
'terms' => $term_ids,
'operator' => 'IN'
)
),
'post__not_in' => array($post->ID),
'showposts' => $count, // Number of related posts that will be shown.
'caller_get_posts' => 1
);
$query = WP_Query($args)
} |
| WordPress Determine Whether 404 Was *Actually* a Private Page View in WordPress » WordPress WP_Query’s get_posts() method takes a... | // Place in 404.php and load subtemplate accordingly
global $wp_query, $wpdb;
if ( count($wpdb->get_results($wp_query->request)) > 0 )
echo 'private post'; |
| WordPress Check User Capabilities on a WordPress Post » | $ptype = get_post_type_object($post_type); // eg. 'post', 'page'
if ( !current_user_can( $ptype->cap->edit_posts ) ) {
wp_die( __( 'You are not allowed to create posts or drafts on this site.' ) );
} |
| WordPress Retrieve a Post From It’s Title » Not entirely obvious from what’s our... | $sql = sprintf( "SELECT * from {$wpdb->posts} WHERE post_title='%s' AND ( post_status <> 'trash' AND post_status <> 'inherit' AND post_status <> 'auto-draft' )", addslashes( $title ) );
$matching_post = get_object_vars( $wpdb->get_row($sql) ); |
| WordPress Get All Attachments to a Post » |
$attachments = get_children('post_type=attachment&post_mime_type=image&post_parent='. $post_id); |