0

I am new to coding a WP_QUERY and I tried a few things with it but it doesn't seem to work. Right now (you can see the page here: http://boomerang.mannydesigns.co/artistes/parcourir/musique/ )

I need to reorganize the ordering of these artists by a custom field I added (with ACF) name exclusif which is a selection field with 3 choices.

Is it possible to do so without doing a WP_QUERY & simply adding to the current code (which is the usual post loop)?

    /**
 * Alters queries
 */
add_action( 'pre_get_posts', 'wp_order_artists' );
function wp_order_artists( $query ) {

    // don't run on the backend
    if ( is_admin() )
      return;

    // Only run on post types artists
    if ( is_post_type_archive('artists') && $query->is_main_query() || $post_type == 'artists' ) {

        $query->set( 'orderby', 'meta_value' );
        $query->set( 'meta_key', 'exclusif' );
        $query->set( 'order', 'DESC' );
    }

    return;
}
if ( have_posts() ) : ?>
    <div class="remix_items grid clearfix <?php echo $artists_col; ?>">
        <?php while ( have_posts() ): the_post(); ?>
            <a class="item<?php $i++; echo ($i % $artists_col_num === 0) ? ' last_item' : ''; ?>" href="<?php the_permalink(); ?>" title="<?php the_title(); ?>" rel="bookmark">
                <figure class="effect-bubba">
                    <?php if ( has_post_thumbnail() ) {
                         the_post_thumbnail('type_cover');
                        } else {
                            echo '<img src="" alt="'.the_title().'">';
                        } ?>
                        <figcaption>
                            <h2><?php the_title(); ?></h2>
                        </figcaption>
                </figure>
            </a>
        <?php endwhile; ?>
   </div>
5
  • 1
    WHY WHY WHY in this world have you switched from pre_get_posts to query_posts query_posts is the WRONG WAY to run a query. Please search this site, I really do think we have said enough about query_posts Feb 24, 2016 at 19:20
  • 1
    There is just no use is using $wp_query if you are breaking it with query_posts. It is like buying a new car for work, then driving it into a wall , scrapping it, for no apparent reason at all Feb 24, 2016 at 19:23
  • 1
    @PieterGoosen I really wish I could upvote that more than once!
    – Cai
    Feb 24, 2016 at 19:26
  • 1
    You should roll back the edit to your question and ask a new question. Your edit is a different problem and my current answer doesn't make sense in relation to the edit. If you ask a new question I'll happily take a look at it.
    – Cai
    Feb 25, 2016 at 13:55
  • @PieterGoosen whether I use query_posts or pre_get_posts the terms I use inside to order will be very similar and the purpose of my question is the boolean I'm trying to order it by prior my other terms. I actually have CAI's code as well as this one for testing purposes - just need some help with the boolean I'm trying to order it by... Feb 25, 2016 at 19:21

1 Answer 1

1

If you want to change the order of an already existing query, you shouldn't create a new WP_Query object but instead use pre_get_posts to alter the existing query, which prevents multiple queries running unnecessarily.

From the pre_get_posts docs:

This hook is called after the query variable object is created, but before the actual query is run.


This is an example of how to change all queries of a custom post type to order by a custom field. Obviously it will need to be changed to fit your situation but should give you an idea of what to do:

/**
 * Alters queries
 */
add_action( 'pre_get_posts', 'wpse_217090_pre_get_posts' );
function wpse_217090_pre_get_posts( $query ) {

    // don't run on the backend
    if ( is_admin() )
      return;

    // Only run on post types 'your_custom_post_type'
    if ( is_post_type_archive('your_custom_post_type') && $query->is_main_query() || $post_type == 'your_custom_post_type' ) {

        $query->set( 'orderby', 'meta_value' );
        $query->set( 'meta_key', '_your_custom_field' );
        $query->set( 'order', 'ASC' );
    }

    return;
}

For reference you should read up on;

10
  • Thank you thank you!!!! That's exactly what I've been looking for. Feb 10, 2016 at 15:37
  • No problem. Glad to help :)
    – Cai
    Feb 10, 2016 at 15:39
  • Check my answer above to confirm I'm on the right track. I added the code just above my if statement on the taxonomy-artist.php template. Exclusif is the ACF field I want to order the items by. Feb 10, 2016 at 17:02
  • The function itself looks fine but it should go in functions.php not the template file
    – Cai
    Feb 10, 2016 at 17:10
  • Is 'artists' a custom post type or custom taxonomy? You need to edit the line if ( is_post_type_archive... to target the correct queries, so if you want it to affect a taxonomy page you will change it to something like if ( is_tax('artist') )
    – Cai
    Feb 10, 2016 at 17:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.