-
-
Save harkor/23a53e40a53908d6de98d505241cc6a8 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Include this page in your functions.php | |
// Flush permalinks | |
// Create 1 Recipe | |
// Create 1 Brand | |
// Create 1 Cheese | |
// On your recipe, associate created brand and created cheese | |
// Adapt id's on adaptQuery function with correct ids | |
// Go to "http://www.domain.com/recipe/ | |
// You will see "Nothing here" | |
// If you comment $brand or $cheese, you will "see" your created recipe | |
// If you change AND by OR, you will see your created recipe twice | |
add_action('init', 'createCustomPostType', 10); | |
add_action('mb_relationships_init', 'myRelationships', 10); | |
add_action('parse_query', 'adaptQuery', 5, 1); | |
function createCustomPostType(){ | |
$labels = array( | |
'name' => 'Recipes', | |
'singular_name' => 'Recipe', | |
'menu_name' => 'Recipes', | |
); | |
$args = array( | |
'labels' => $labels, | |
'description' => 'Recipes', | |
'public' => true, | |
'publicly_queryable' => true, | |
'show_ui' => true, | |
'show_in_menu' => true, | |
'query_var' => true, | |
'capability_type' => 'post', | |
'has_archive' => true, | |
'hierarchical' => false, | |
'menu_position' => null, | |
'supports' => array( | |
'title', | |
'editor', | |
) | |
); | |
register_post_type('recipe', $args); | |
$labels = array( | |
'name' => 'Brands', | |
'singular_name' => 'Brand', | |
'menu_name' => 'Brands', | |
); | |
$args = array( | |
'labels' => $labels, | |
'description' => 'Brands', | |
'public' => true, | |
'publicly_queryable' => true, | |
'show_ui' => true, | |
'show_in_menu' => true, | |
'query_var' => true, | |
'capability_type' => 'post', | |
'has_archive' => false, | |
'hierarchical' => false, | |
'menu_position' => null, | |
'supports' => array( | |
'title', | |
'editor', | |
) | |
); | |
register_post_type('brand', $args); | |
$labels = array( | |
'name' => 'Cheeses', | |
'singular_name' => 'Cheese', | |
'menu_name' => 'Cheeses', | |
); | |
$args = array( | |
'labels' => $labels, | |
'description' => 'Cheeses', | |
'public' => true, | |
'publicly_queryable' => true, | |
'show_ui' => true, | |
'show_in_menu' => true, | |
'query_var' => true, | |
'capability_type' => 'post', | |
'has_archive' => false, | |
'hierarchical' => false, | |
'menu_position' => null, | |
'supports' => array( | |
'title', | |
'editor', | |
) | |
); | |
register_post_type('cheese', $args); | |
} | |
function myRelationships(){ | |
MB_Relationships_API::register( [ | |
'id' => 'recipes_to_brands', | |
'from' => 'recipe', | |
'to' => 'brand', | |
]); | |
MB_Relationships_API::register( [ | |
'id' => 'cheeses_to_recipes', | |
'from' => 'cheese', | |
'to' => 'recipe', | |
]); | |
} | |
function adaptQuery($query){ | |
if ( !is_admin() && $query->is_main_query() ) { | |
if ( is_post_type_archive('recipe')) { | |
$cheese = 17; // My cheese ID | |
$brand = 18; // My brand ID | |
$relationShipArgs = []; | |
if(isset($brand)): | |
$relationShipArgs[] = [ | |
'id' => 'recipes_to_brands', | |
'to' => $brand, | |
]; | |
endif; | |
if(isset($cheese)): | |
$relationShipArgs[] = [ | |
'id' => 'cheeses_to_recipes', | |
'from' => $cheese, | |
]; | |
endif; | |
if(sizeof($relationShipArgs) > 0): | |
$relationShipArgs['relation'] = 'AND'; | |
$query->set('relationship', $relationShipArgs); | |
endif; | |
} | |
} | |
return $query; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment