Home › Forums › General Issues › searching results
How do I get into the field of search results for the latest recording is displayed instead of the old records?
http://screenshot.net/ngno8tm
Hi @inforesist
Is it Post Object field? If it is, you can modify the query using the “acf/fields/post_object/query” hook. Please take a look at this page to learn more about this hook: http://www.advancedcustomfields.com/resources/acf-fields-post_object-query/.
Hope this helps.
I do not quite appropriate. I need to show the most new news in descending order.
Hi @inforesist
If it is a Post Object field, you need to add this function into your functions.php file:
function my_post_object_query( $args, $field, $post )
{
// modify the order
$args['orderby'] = 'date';
$args['order'] = 'DESC'; // change to 'ASC' to reverse it
return $args;
}
// filter for a specific field based on it's name
add_filter('acf/fields/post_object/query/name=my_select', 'my_post_object_query', 10, 3);
Please change “my_select” with your field name.
To learn more about functions.php, please take a look at this page: https://codex.wordpress.org/Functions_File_Explained.
I hope this clear enough.
Hi @inforesist
What if you change the ‘DESC’ to ‘ASC’. Are there any changes? If there aren’t any changes, could you please share the JSON or XML export of your field group?
Also, could you please tell me where do you show the search result? Is is on the backend or front end?
Hi @inforesist
Thanks for the JSON export. That makes all clear. It seems that you are using relationship field instead of post object field. For this field type, you need the “acf/fields/relationship/query” hook instead. It should be like this:
function my_relationship_query( $args, $field, $post_id ) {
{
// modify the order
$args['orderby'] = 'date';
$args['order'] = 'DESC'; // change to 'ASC' to reverse it
return $args;
}
// filter for a specific field based on it's name
add_filter('acf/fields/relationship/query/name=for_theme', 'my_relationship_query', 10, 3);
This page should give you more idea about it: http://www.advancedcustomfields.com/resources/acf-fields-relationship-query/.
I hope this helps.
Hi @inforesist
I’m sorry about that. Please try this code instead:
function my_relationship_query( $args, $field, $post_id ) {
// modify the order
$args['orderby'] = 'date';
$args['order'] = 'DESC'; // change to 'ASC' to reverse it
return $args;
}
// filter for a specific field based on it's name
add_filter('acf/fields/relationship/query/name=for_theme', 'my_relationship_query', 10, 3);
Hope this helps!
The topic ‘searching results’ is closed to new replies.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.