First, you should stop using query_posts()
. As said in the documentation:
Note: This function isn’t meant to be used by plugins or themes. As
explained later, there are better, more performant options to alter
the main query. query_posts() is overly simplistic and problematic way
to modify main query of a page by replacing it with new instance of
the query.
You can find a more comprehensive explanation in this answer by @Rast.
For new loops, as you are doing in the description of your question, it is better to use WP_Query
class. With this class you can use the cat
paramter to get posts from specific categories and combine meta_key
(or meta_query
) with order
and orderby
parameter to sort by a custom field. It doesn’t if the custom field is created by ACF or by any other way, all you need is the key of the meta field.
$args = array (
'cat' => '122,123,124',
// Change the_identifier_key_of_the_field with the identifier of your field
'meta_key' => 'the_identifier_key_of_the_field',
// Set order to ASC or DESC
'order' => 'ASC',
// Use 'orderby' => 'meta_value_num' for numeric meta fields
'orderby' => 'meta_value',
);
$query = new WP_Query( $args );
if( $query->have_posts() ) {
while( $query->have_posts() ) {
$query->the_post();
?>
<a href="https://wordpress.stackexchange.com/questions/197036/<?php the_permalink(); ?>"><?php the_title (); ?></a>
<?php
}
}
// Reset original post data after the secondary loop
wp_reset_postdata();