Inicio › Toolset Professional Support › [Resuelto] Hide Posts with Certain Meta from All Views
Problem:
Client has a custom field which, if set to some value, means that the post should be hidden in a View to all users except admin users. How to implement?
Solution:
Custom code is required, using the wpv_filter_query API hook. The suggested code is listed below (https://toolset.com/forums/topic/hide-posts-with-certain-meta-from-all-views/#post-919332) and is provided by way of demonstration, you may need to update to your needs.
Relevant Documentation:
https://wp-types.com/documentation/programmer-reference/views-filters/#wpv_filter_query
This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.
Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.
Hoy no hay técnicos de soporte disponibles en el foro Juego de herramientas. Siéntase libre de enviar sus tiques y les daremos trámite tan pronto como estemos disponibles en línea. Gracias por su comprensión.
Sun | Mon | Tue | Wed | Thu | Fri | Sat |
---|---|---|---|---|---|---|
- | 7:00 – 14:00 | 7:00 – 14:00 | 7:00 – 14:00 | 7:00 – 14:00 | 7:00 – 14:00 | - |
- | 15:00 – 16:00 | 15:00 – 16:00 | 15:00 – 16:00 | 15:00 – 16:00 | 15:00 – 16:00 | - |
Supporter timezone: Europe/London (GMT+00:00)
Etiquetado: Views API, Views plugin
Documentación relacionada:
Este tema contiene 5 respuestas, tiene 2 mensajes.
Última actualización por aaronM-9 hace 6 años, 5 meses.
Asistido por: Nigel.
I would like to be able to hide any post that has the custom field wpcf-development-hide-content from any and all views output unless the currently logged in user is in the administrator role. I was going to use the wpv_filter_query hook but am not 100% sure on how to use it. I've go the following code as a starting point but can you assist in telling me how to use it add a filter to the view?
add_filter( 'wpv_filter_query', 'be_hide_development_items' ); function be_hide_development_items( $query_args ) { if ( current_user_can('administrator') ) { } else { $query_args['???'] = // some code here to add filter wpcf-connection-hide-content != 1 (or is null) } return $query_args; }
Thanks for any help.
- Aaron
Idiomas: Inglés (English ) Español (Español )
Zona horaria: Europe/London (GMT+00:00)
Hi Aaron
Can you just clarify, this field 'wpcf-development-hide-content ', it may or may not be present on all posts, and it can have what possible values?
Hi Nigel,
Certainly. The possible values are 1 or 0, although I can't be 100% certain all posts will have the variable at all. I have set the custom field to apply to all posts and have a default value of 0 but it wouldn't be there for older posts and I'm not sure it will add the field if I don't explicitly put it into a front-end form. So, to be safe, I'd say the post should be filtered out of a view if the field exists and it equals 1. Otherwise, the post should not be filtered out.
- Aaron
Idiomas: Inglés (English ) Español (Español )
Zona horaria: Europe/London (GMT+00:00)
This is complicated somewhat by the fact that using the != comparison for a meta_query will exclude posts that don't have the key at all, so special allowance needs to be made for that.
Try this:
function tssupp_hide_posts( $view_args, $view_settings, $view_id ){ if ( !current_user_can( 'manage_options' ) ) { // only do this for non-admins $new_args = array ( 'relation' => 'OR', array( 'key' => 'wpcf-development-hide-content', 'compare' => 'NOT EXISTS' ), array( 'key' => 'wpcf-development-hide-content', 'value' => 1, 'compare' => '!=' ) ); if ( isset( $view_args['meta_query'] ) ) { $view_args['meta_query'][] = $new_args; } else { $view_args['meta_query'] = $new_args; } } return $view_args; } add_filter( 'wpv_filter_query', 'tssupp_hide_posts', 101, 3 );
Tried it out and it is filtering everything out of all views, so no content comes up. It's doing this regardless of role (tested it as guest, subscriber, and admin).
- Aaron
I take that back. I did something silly when pasting in your code. It actually works like a charm. Tested as guest, subscriber, and admin and seems to be working in all scenarios. Thanks so much!
- Aaron