Problem: I have a Form that edits posts, which includes a custom field. I would like to automatically set a taxonomy term on the post when the Form is submitted. The term should be determined by the value of the custom field.
Solution: Use the function wp_set_object_terms to set a term on a post:
Instead of term-slug you can use:
A single term slug, single term id, or array of either term slugs or ids. This will replace all existing related terms in this taxonomy. Passing an empty value will remove all related terms.
The last parameter false will delete all the other category terms from this post. If you use true instead of false, the existing category terms will not be deleted.
Problem: I have a custom role that should be able to submit a Form to add or edit a child post, but the parent post options are not shown in the select field in the Form. This was working before a recent update.
Solution: Check your custom code to confirm that redirects set up to block access to the back-end of the site are not applied to admin-ajax.php requests. Add some conditional logic to prevent those redirects as needed, like in the following code example:
/**
* Block wp-admin access for non-admins (not while doing AJAX, see https://toolset.com/forums/topic/custom-role-no-longer-able-to-perform-same-tasks/)
*/
function ace_block_wp_admin() {
if (!current_user_can('edit_users') && ( !defined('DOING_AJAX') || !DOING_AJAX ) ) {
wp_safe_redirect( '/my-account');
exit;
}
}
add_action( 'admin_init', 'ace_block_wp_admin' );
Problem: I have two nested Views set up to display a list of child posts. If no child posts are found, I would like to display "No items found" once. However, if I add the "No items found" message in the child View, it is repeated for each parent post. If I add it in the parent View, it is never shown because there are always parent posts.
Solution: There is not a simple way to set this up because each child View is rendered independently of the other child Views. There is no built-in way to keep a running total of all child posts across the nested Views, so a custom code solution in the classic Views editor is required. You must use a custom shortcode to add the number 1 each time a child post is displayed. Then use a conditional to test the total amount after all the child post loops have run.
First, add these two custom shortcodes in your child theme's functions.php file, or in a new custom code snippet in Toolset > Settings > Custom Code:
// https://toolset.com/forums/topic/not-found-message/
global $total;
function add_total_shortcode($atts, $content = '') {
global $total;
$total += wpv_do_shortcode($content);
}
add_shortcode('add-to-total', 'add_total_shortcode');
function show_total_shortcode( $atts, $content ) {
global $total;
$atts = shortcode_atts( array(
'decimals' => 0
), $atts );
$totalNew = $total;
$total = 0;
return number_format($totalNew, (int) $atts['decimals'] );
}
add_shortcode('show-total', 'show_total_shortcode');
Go to Toolset > Settings > Front-end Content and register show-total in Third-party shortcode arguments.
In your child View, insert the following code somewhere inside the wpv-loop tags:
[add-to-total]1[/add-to-total]
Then in the parent View, insert the following conditional shortcode somewhere outside the wpv-loop tags but inside the wpv-items-found shortcode:
[wpv-conditional if="( '[show-total][/show-total]' gte '1' )" evaluate="false"]
There are NO child posts
[/wpv-conditional]
You may replace the text with your own custom message as needed.
Problem: I would like to display fields from one custom post type in another custom post type by relating posts to one another.
Solution: You can use a post reference field to establish a link between two posts in different post types. Then you can use a Fields and Text block to insert fields from a related post in the Content Template.
Problem: I have two post types in a one-to-many post relationship, where Department is the parent post type and Service is the child post type. I would like to create a grid showing one parent Department post in each grid cell. Inside each grid cell I would also like to display a list of all the child Services for that parent Department.
Solution: Use nested Views to accomplish this in the legacy Views editor. The general process:
- Create a new View of Departments, choosing to "display all results" in the initial popup
- Use the Loop Wizard to create a Bootstrap grid design or a table-based grid design if your site is incompatible with Bootstrap
- Insert the Post Title or Post Title with Link in the loop for now, we will add the Services list a bit later
- Save the View and insert it in a custom post or page wherever you want to display this directory
- Verify the grid of Departments is displayed as expected and make any grid adjustments needed
- Create a new View of Services, choosing to "display all results" in the initial popup
- Add a Query Filter for post relationships, set for items in the Department / Services relationship as related items of...The current post in the loop
- Use the Loop Wizard to design an unordered list loop and insert the Post Title or Post Title with Link in the loop
- Save the View of Services and return to edit the View of Departments
- In the Loop Editor, place the cursor just after the post title and use the Fields and Views button to insert the View of Services
- Save the View of Departments and check the results on the front-end of the site