Skip Navigation

[Resuelto] view order by custom field

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

Problem:
view order by custom field

Solution:
User wants to order the posts based on custom field. He has added the default value for custom field but adding default value does not reflect to database as it will be only get saved when you save/update the post.

You can find proposed solution, in this case with the following reply:
https://toolset.com/forums/topic/orden-personalizado-para-view/#post-2228839

Relevant Documentation:

This support ticket is created hace 3 años. 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.

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
- 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Kolkata (GMT+05:30)

Este tema contiene 7 respuestas, tiene 2 mensajes.

Última actualización por aitorS hace 3 años.

Asistido por: Minesh.

Autor
Mensajes
#2226791

He añadido un campo personalizado para mis CPT para usarlo para ordenar el contenido según necesite en mis views, funciona correctamente pero me encuentro con el problema de que si un CPT no tiene introducido nada en ese campo personalizado para ordenar no aparece en el resultado de la view ¿existe alguna forma de hacer que se ordene por ese campo y que los CPT que no tengan ningún valor introducido aparezcan después de las que si tienen el campo introducido?

#2226873

Minesh
Supporter

Idiomas: Inglés (English )

Zona horaria: Asia/Kolkata (GMT+05:30)

Hello. Thank you for contacting the Toolset support.

Spanish support is not available at the moment and I'll try to help you in English language. I hope that is ok.

Actually, that is how WordPress works. When you set your view to order by custom field, it will only include the posts where value is exists. All posts where you do not setup the value for that custom field are skipped.

The workaround would be to add default value to your order by field or we can grab all the posts where field value is not exists and pass on those IDs on fly using the filter hooks offered by views but again that workaround will be subject to how complex view you are using.

If you can share problem URL and access details once I will review your setup I will be able guide you in the right direction.

*** Please make a FULL BACKUP of your database and website.***
I would also eventually need to request temporary access (WP-Admin and FTP) to your site. Preferably to a test site where the problem has been replicated if possible in order to be of better help and check if some configurations might need to be changed.

I have set the next reply to private which means only you and I have access to it.

#2226931

Minesh
Supporter

Idiomas: Inglés (English )

Zona horaria: Asia/Kolkata (GMT+05:30)

I can see you are using the custom search view with multiple frontend filters and also using the infinite scrolling pagination.

Could you please tell me what posts you want to display first and what posts you want to display last. As when I check every posts of post type "Curso" I can see you setup a value for every post for the custom field "Orden en categoría".

#2226943

As you can see currently only 6 courses are shown in the view, these are the ones that I entered the value of "Order in category" the rest of the courses do not have the value entered and are not displayed.

#2227877

Minesh
Supporter

Idiomas: Inglés (English )

Zona horaria: Asia/Kolkata (GMT+05:30)

I see that you have set 999 as default value to your custom field. Adding default value does not reflect to database as it will be only get saved when you save/update the post.

I would like to know, what posts you want to display first - the ones which have default value set to 999 those posts you want to display first and then other posts? if that is correct I will have to run a custom query on your postmeta table to add a the meta key for the custom field "Orden en categoría".

Can you just confirm what posts you want to display first and accordingly I will build the query and run on your database table.

#2227969

I want to show the posts that have the smallest value first, if you build the query to set the value for all CPTs I run it, I would set it for all to 999 and then we will modify as appropriate.

#2228839

Minesh
Supporter

Idiomas: Inglés (English )

Zona horaria: Asia/Kolkata (GMT+05:30)

Here is the query that will help you to find out the posts where there is no value setup for the custom field "Orden en categoría".

It will return you the post IDs where the custom field "Orden en categoría" is not setup.

SELECT udeka_posts.ID
FROM udeka_posts  
LEFT JOIN udeka_postmeta ON (udeka_posts.ID = udeka_postmeta.post_id AND udeka_postmeta.meta_key = 'wpcf-orden-en-categoria')
WHERE 1=1 AND udeka_posts.post_type = 'cursos_udeka' AND (udeka_posts.post_status = 'publish')  AND (udeka_postmeta.post_id IS NULL) GROUP BY udeka_posts.ID

Now, you will have to loop through the each post ID and add the postmeta key "wpcf-orden-en-categoria" with your desired value to the post meta table.

I tried to build the code snippet that I should run but when I try to do that and when I try to save the snippet it tell me I do not have permissions to add the snippet.

If you can grant me FTP access details I'm happy to help otherwise you can do it on your own.

#2229077

My issue is resolved now. Thank you!