Quantcast
Channel: WordPress › Support » Forum: Hacks - Recent Topics
Viewing all articles
Browse latest Browse all 8245

shaunny on "Unable to filter single post query"

$
0
0

I have a client with a theme that adds extra post types; artists and artworks.
One of the meta values for artworks is the artist, and it links the two post types together, as opposed to using a taxonomy instead.

I've created a plugin that filters artworks on the admin pages by the artist in question, however, when it is filtered, it also filters all post queries, including the one that populates my dropdown box.

Because the Artists post type does not have the same meta key, all query results are null, and I'm unsure how to get it to distinguish between the two.

The full code can be found below:

<?php
/**
 * Plugin Name: Artwork Filter by Artist
 * Author: Shaun Cockerill
 * Verion: 0.1
 * License: GPL2
**/

function restrict_artworks_by_artist() {
    global $typenow;
    global $wp_query;
    if ($typenow=='artworks') {
        wp_dropdown_users();
    }
}
add_action('restrict_manage_posts','restrict_artworks_by_artist');

function wp_dropdown_artists_modified( $args = '' ) {
	$re = '<select name="artist" id="artist"><option value="0">Show All Artists</option>';
	$posts_array = get_posts( array( 'post_type' => array( 'artists' ), 'posts_per_page' => -1 ) );
	foreach ( $posts_array as $post ) {
		if ( isset( $_GET['artist'] ) && ( $post->{'ID'} == $_GET['artist'] ) ) {
			$re .= '<option value="' . $post->{'ID'} . '" selected="selected">' . $post->{'post_title'} . '</option>';
		}
		else {
			$re .= '<option value="' . $post->{'ID'} . '">' . $post->{'post_title'} . '</option>';
		}
	}
	$re .= '</select>';
	return $re;
}
add_filter('wp_dropdown_users', 'wp_dropdown_artists_modified');

function ba_admin_posts_filter( $query )
{
    global $pagenow;
    global $typenow;
    if ($typenow=='artworks' && !empty($_GET['artist']) && is_admin() && $pagenow=='edit.php' && is_main_query()) {
	$query->query_vars['meta_key'] = 'artist';
	$query->query_vars['meta_value'] = $_GET['artist'];
    }
}
add_filter( 'parse_query', 'ba_admin_posts_filter' );

I have tried adding && $query->query['post_type'][0]=='artworks' to my if statement within ba_admin_posts_filter, however it no longer filters any queries.


Viewing all articles
Browse latest Browse all 8245

Trending Articles