Saltar navegación

[Resuelto] Tutorial sample view hooks

Este hilo está resuelto. Aquí tiene una descripción del problema y la solución.

Problem:

The problem here is that the user is using the wpv-after-display-post hook in order to do a views counter on his post. This means that the counter should increment each time the post is loaded on the page using views.

The code that the user is using can be seen below.

// Add our callback
// Mind that we set a priority of 2 and we expect 2 arguments
add_action( 'wpv-after-display-post', 'my_count_on_views', 10, 2 );
// Define our callback
function my_count_on_views( $post, $view_id ) {
if ( $view_id == 207615 ) {
// Only do something if the current View is our View, with ID 44
$counter = get_post_meta( $post->ID, 'my_counter', true );
$counter = $counter + 1;
update_post_meta( $post->ID, 'my_counter', $counter );
}
}

The user is storing the counter inside a custom field but the field itself is not being updated with the value.

Solution:

The solution is that since you are using a custom field created using Types then you will need to user the wpcf- prefix when you are referencing the field using the wordpress post meta functions.

In this users case he should've retrieve the field like this wpcf-my_counter.

This support ticket is created hace 6 años, 11 meses. There's a good chance that you are reading advice that it now obsolete.

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.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 -
- 13:00 – 18:00 13:00 – 18:00 13:00 – 18:00 14:00 – 18:00 13:00 – 18:00 -

Supporter timezone: America/Jamaica (GMT-05:00)

Etiquetado: ,

Este tema contiene 2 respuestas, tiene 2 mensajes.

Última actualización por Stephen Vaughan hace 6 años, 11 meses.

Asistido por: Shane.

Autor
Mensajes
#611125

Tell us what you are trying to do?:
Learning about the API, View hooks specifically

Is there any documentation that you are following?:
https://toolset.com/documentation/programmer-reference/toolset-hooks/

Hi,

I am going through the documentation, testing the examples given to see how it works.

I am not seeing any result using the following.

// Add our callback
// Mind that we set a priority of 2 and we expect 2 arguments
add_action( 'wpv-after-display-post', 'my_count_on_views', 10, 2 );
// Define our callback
function my_count_on_views( $post, $view_id ) {
if ( $view_id == 207615 ) {
// Only do something if the current View is our View, with ID 44
$counter = get_post_meta( $post->ID, 'my_counter', true );
$counter = $counter + 1;
update_post_meta( $post->ID, 'my_counter', $counter );
}
}

I am saving this in my functions file. I am applying this to the regular posts on a localhost site and I have added a custom field for my_counter (number). I made a custom template in Views (Divi method) for Posts and included the my_counter field. Next I made a custom view for posts and have tried filters for title search and categories, separately, to test this, but I don't see any figures appear where the my_counter field is.

My understanding is that any time a post appears through a search that its my_counter should increment by 1.

Am I doing something wrong? I tested the next example (for filters: a template for every day of the week) and it works.

#611141

Shane
Supporter

Idiomas: Inglés (English )

Zona horaria: America/Jamaica (GMT-05:00)

Hi Stephen,

Thank you for contacting our support forum.

So you essential want to keep a track of the number of views the posts are getting correct?

// Add our callback
// Mind that we set a priority of 2 and we expect 2 arguments
add_action( 'wpv-after-display-post', 'my_count_on_views', 10, 2 );
// Define our callback
function my_count_on_views( $post, $view_id ) {
if ( $view_id == 207615 ) {
// Only do something if the current View is our View, with ID 44
$counter = get_post_meta( $post->ID, 'wpcf-my_counter', true );
$counter = $counter + 1;
update_post_meta( $post->ID, 'wpcf-my_counter', $counter );
}
}

Was the counter field created in our Types plugin ? If so then you will need to use the wpcf- prefix before the custom field slug.

Please let me know if this works now.

Thanks,
Shane

#611172

Hi Shane,

Prepending with wpcf- did the trick.

Perhaps an update to the documentation to flag this would be a good idea.