Query custom post type by custom field
General Info
I used the plugin WP Types (wp-types.com) to create a custom post type called "artist".
In WP Types you can also set another post type as parent post type. This is not possible by default; they implemented this feature by a meta query with the key:
"_wpcf_belongs_%POSTTYPE%_id" (in my case "_wpcf_belongs_artist_id")
and the value is the ID of the parent post.
The Problem
Normally if you set a page X as a child of page Y you would have the following permalink structure:
/page-y/page-x/
I thought this plugin would adopt this but it does not. For example I use "painting" with the ID 24 as the parent page and "picasso" with the ID 72 as the child post with the post type "artist".
So this:
/painting/picasso/
does redirect me to:
/artist/picasso/
but outputs the right content.
My solution approaches
As the paid support could not help me I have to try on my own. For writing my own "custom" rewrite rules I have to know the correct query at first. I tried to get my content manually:
index.php?_wpcf_belongs_artist_id=24artist=picasso
redirects me to:
/artist/picasso?_wpcf_belongs_artist_id=24artist=picasso
with the correct output of the single artist post.
This:
index.php?_wpcf_belongs_artist_id=24
just outputs my front page.
The only more or less working variant is:
/painting/picasso/?artist=picasso_wpcf_belongs_artist_id=24 and:
/painting/picasso/?artist=picasso
These don't redirect me to /artist/picasso/… or anywhere but output the correct single artist post.
Here you can find some more variants and the whole thread in the support forums of WP Types.
First step probably would be to get the right content with a queryvar like "parent_id". After that I can start dealing with the rewrite rules itself, there might be problems because of the double usage of the top level (%pagename% and %parent_id%). This probably will confuse WordPress.
UPDATE 12/17
I tried this code to get all posts with the meta field '_wpcf_belongs_artist_id' and the meta value retrieved from the URL (/?parent_id=24). Unfortunately, it doesn't seem to work. It crashes my submenu's auxiliary queries and outputs just the result of the normal query.
add_filter('query_vars', 'custom_query_vars');
function custom_query_vars($vars) {
$vars[] = 'parent_id';
return $vars;
}
function parent_query( $query ) {
// Exclude
if( $query-is_admin == 1 !$query-is_main_query() !$query-is_archive == 1 ) {
return;
}
// retrieve field name / value from URL if exist
$custom_field = '_wpcf_belongs_artist_id';
$custom_value = ( $_GET['parent_id'] ) ? stripslashes( $_GET['parent_id'] ) : '';
if( $custom_field ) {
// add meta key requirement -- we’ll return all posts with some ‘color’
$query-set( 'meta_key', '_wpcf_belongs_artist_id' );
if( $custom_value ) {
// build meta value requirement -- we’ll return only blue
$query-set( 'meta_value', $custom_value );
}
}
}
add_action( 'pre_get_posts', 'parent_query' );
Topic plugin-types custom-field query custom-post-types Wordpress
Category Web