Support

Account

Home Forums Backend Issues (wp-admin) Filtering custom posts by post object field

Helping

Filtering custom posts by post object field

  • Hello, I’m working on a website where I registered 2 custom post types : artist and release. I’ve created a bi-directional post object field : related. I’ve also added custom columns for each post type.

    Custom fields for the post type artist :
    Custom fields for artists

    Custom fields for the post type release :
    Custom fields for releases

    Artist admin page :
    Artist admin page

    Release admin page :
    Release admin page

    I’ve managed to filter artists by the column “Origin”

    Here is the code I used to filter the artists by origin :

    function meta_key_filter( $query ) {
    
    		if( isset( $_GET['post_type'] ) ) {
    			$post_type = $_GET['post_type'];
    		}
    
    		if( $query->is_admin && isset( $_GET['origin'] ) && $post_type === 'artist' ) {
    			$origin = $_GET['origin'];
    			$meta_key_query = array(
    				array(
    					'key' => 'origin',
    					'value' => $origin,
    					'compare' => 'LIKE',
    				)
    			);
    			$query->set( 'meta_query', $meta_key_query );
    		}
    		return $query;
    
     }
    }
    add_action( 'pre_get_posts', 'meta_key_filter' );

    What I’d like to achieve is filtering releases by the column “Artist” but I can’t figure out to do so.

    Any advise?

  • I’m facing the exact same issue of not being able to filter by ANY custom post type. Have you figured anything out in relation to this that you’d like to share?

Viewing 2 posts - 1 through 2 (of 2 total)

The topic ‘Filtering custom posts by post object field’ is closed to new replies.