Silverstripe Filter by parent page properties - silverstripe

When querying the database via dataobject orm, how can records be filtered by the properties of the page's parent?
So what I have in mind is something like:
$facilities_by_keyword = Facility::get()->filter('Parent.tags:partialmatch', $tag);

You could get the all parent pages first. Then get the Facility pages that are children of any of those parent pages:
$parentPages = Page::get()->filter('Parent.tags:partialmatch', $tag);
$parentIDs = $parentPages->getIDList();
$facilities_by_keyword = Facility::get()->where('ParentID IN (' . implode(',', $parentIDs) . ')');

Related

Wordpress - custom template page for a parent category and all child categories

In WP 5.4.2 I want to create a custom archive page for a category and all it's child categories. I am aware of the template file hierarchy:
1. category-slug.php
2. category-ID.php
3. category.php
4. archive.php
5. index.php
but if I understand correctly and if I did all the testing right, the category-slug.php, or the category-id.php scheme applies to a single category regardless of the category hierarchy.
Let's say I have following categories:
colors (id 2)
- red (id 10)
- green (id 11)
- blue (id 12)
I need a single template file for all of them. Simply creating category-colors.php or category-2.php doesn't work. It applies to the single category (colors) only. I want it to apply to all the current child categories, as well as all the child categories I add in the future. Is it possible? If so, please advice how.
There are a couple of ways to do this, but using the category_template filter to let you use a custom category template seems the most common.
The function below will let you dynamically check back through the parent levels of the current category until it finds a template called "category-[parent slug]" for the closest ancestor category or it reaches the top level - whichever is first.
Suppose you have something like:
- products
- hardware
- food
- dairy
- vegetables
On the dairy page, it will first check if you have a slug called category-dairy.php.
If you do, it will return it.
If you don't, it will look for category-food.php.
If that's not found, it will look for category-products.php.
Add this to your functions.php - this is untested by the code is well commented so you can understand how it works:
function get_template_for_category( $template ) {
if ( basename( $template ) === 'category.php' ) { // No custom template for this specific term, let's find it's parent
// get the current term, e.g. red
$term = get_queried_object();
// check for template file for the page category
$slug_template = locate_template( "category-{$term->slug}.php" );
if ( $slug_template ) return $slug_template;
// if the page category doesn't have a template, then start checking back through the parent levels to find a template for a parent slug
$term_to_check = $term;
while ( $term_to_check ->parent ) {
// get the parent of the this level's parent
$term_to_check = get_category( $term_to_check->parent );
if ( ! $term_to_check || is_wp_error( $term_to_check ) )
break; // No valid parent found
// Use locate_template to check if a template exists for this categories slug
$slug_template = locate_template( "category-{$term_to_check->slug}.php" );
// if we find a template then return it. Otherwise the loop will check for this level's parent
if ( $slug_template ) return $slug_template;
}
}
return $template;
}
add_filter( 'category_template', 'get_template_for_category' );
References:
WP Codex category_template Reference
WP Developer locate_template Reference
Make categories use parent template

Add new Wordpress page with preselected parent

I do not want my clients to select a parent page from the drop down list, when creating a child page in Wordpres, so I would like to now if there is a way to create a link on the dashboard which links to "add new page" - but with a preselected parent page?
If this is not possible, then is there a way to change the default parent from "(no parent)" to a parent of my choice?
Put this script in your plugins folder and it'll give new pages a parent of whatever you set as $parent_id
<?php
function set_parent($content) {
/* >> Begin user-configurable variable >> */
$parent_id = '1'; // ID for parent page >> */
/* >> End user-configurable variable >> */
$pattern = "option value='{$parent_id }'";
$replace = "option value='{$parent_id }' selected=\"selected\"";
$content = str_replace($pattern, $replace, $content);
return $content;
}
if( strstr($_SERVER['REQUEST_URI'], 'wp-admin/post-new.php?post_type=page') ) {
ob_start('set_parent');
}
?>
That should do what you want. And I suppose you could add a dropdown option in somewhere to select parents if a set default parent isn't sufficient like in this example.
This is a tough question to answer sticking with core wordpress ideas. Would you normally have more than one parent page available?
Do you think that about a different way for your client to manage page structure would work?
Admin Column View Plugin is great and allows the user to use drag and drop to reorder pages - http://wordpress.org/plugins/admin-column-view/screenshots/
Hope this helps.

list child page items (1 level with meta keys)

I need to have a index page and it can pull out the title (with link), meta key description and meta key image (meta keys from custom fields) of its child pages, but only one level down.
e.g. I have a index page call WORK, and few child index PRINT, WEB. there will be some pages under PRINT and WEB as well, but I don't want them to be shown on the WORK index. I only need to have PRINT and WEB to be listed and with the meta keys.
Anybody can help please? many thanks!
Try putting:
query_posts("cat=1,-2,-3");
...Before the loop. Replace the numbers after cat= with the category IDs in question; using the negative symbol (such as "-2,-3") excludes a category.
To do this more programmatically:
$parent = 1; //catID of parent
$children = get_categories("child_of=".$parent);
foreach ($children as $child) {$excluded .= "-".$child->term_id.","; //add minus sign}
$excluded = rtrim($excluded, ","); //remove last comma
query_posts("cat=".$parent.",".$excluded);

Wordpress - Get Current Category Parents

In category.php, how would I test against a category having a parent category?
If the parent category is A, and the sub-category is B, and the user loads the URL for category B, I would like to be able to test if it has parent A, and run code if it does.
I've found the get_category_parents tag, but it seems to return a link list rather than an array:
get_category_parents($cat, TRUE, ', '));
Even if I got the array, I'm not sure what the php function is to test against it (php noob).
Thanks!
You can use the cat_is_ancestor_of function to check if a category is a child of another category. For example to check if the current category is a child of a category named 'blog':
cat_is_ancestor_of(get_cat_id('blog'), get_query_var('cat'))
First, you have to get current category's id:
$category_id = get_query_var('cat');
Then you can make a database query to see it has a parent or not:
$parent = $wpdb->get_var("SELECT parent FROM ".$wpdb->prefix."term_taxonomy WHERE term_id = $category_id");
If it has one, $parent will contain one-level-above parent's id and naturally it's value should be greater than 0, so you can check this like below:
<?php if ($parent > 0 ) : ?>
// do something
<?php endif; ?>
You can use $parent however you want;
$parent_link = get_category_link( $parent );
$parent_name = get_cat_name( $parent );
// etc.
If you want to see that there is more high-level parent's, you can do the same things(db query) with parent's id or even write a recursive function that go to until top level. As i saw in the source code, built-in get_category_parents() function do it like that.
HTH
Check out the reference page for get_category_parents, you are asking for links when you set the second parameter to TRUE.
You should be doing:
get_category_parents($cat, FALSE, ', '));
I also recommend not using spaces as separators, since you will probably need to trim those spaces later.

How do I get the parent category name in WordPress template? And can I query post by the parent category?

I tried getting help on the WordPress forums but no luck. Anyways, here is my question...
Lets say I am creating 10 parent categories and 2 sub categories to each parent. My WordPress post belongs to one sub category of a particular parent category
How do I get the parent category name ONLY? I don't want subcategories names? what WordPress code would do that?
And one more question...
Is it possible to query post by the parent of a sub category by using:
but instead of entering cat=1 or the name of the particular category, can I do something like:
So this way it would automatically insert and query post for the parent of any particular sub category that's clicked on?
To get the parent category name, use the get_cat_name() function, with the parent as the parameter -- like so:
$cat = get_the_category();
$parentCatName = get_cat_name($cat[0]->parent);
All these answers failed for me.
I eventually managed to display a post's topmost category name like this :
$categories = get_the_category();
$category= '';
foreach($categories as $childcat) {
$parentcat = $childcat->category_parent;
if($parentcat>0){
$category = get_cat_name($parentcat);
continue;
}
}
$category = (strlen($category)>0)? $category : $categories[0]->cat_name;
Found this answer, which gives you the first ancestor slug. It could easily be modified to give you the name.
Got it here: http://nick.boldison.com/wordpress/wordpress-get-top-level-parent-category/
<?php
// get parent category slug
$parentCatList = get_category_parents($cat,false,',');
$parentCatListArray = split(",",$parentCatList);
$topParentName = $parentCatListArray[0];
$sdacReplace = array(" " => "-", "(" => "", ")" => "");
$topParent = strtolower(strtr($topParentName,$sdacReplace));
?>
In fact, to get the parent name:
// get parent category slug
$parentCatList = get_category_parents($cat,false,',');
$parentCatListArray = split(",",$parentCatList);
$topParentName = $parentCatListArray[0];
Lots of answers and examples in the Wordpress docs:
get category parents
get the category
(And it looks like some code snips or other text didn't come through in your original question)

Resources