0
votes

I am using the acf wordpress plugin to add a boolean field (featured_project) to a custom post(project).

I am trying to sort the posts on the category archive page, to show the posts that are featured at the top and non featured at the bottom.

There are multiple categories with the same type of post.

I have read some other similar problems where the solutions were to use 'wp_query' or pre_get_posts but I can't seem to get it to work.

<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
    <?php if ( get_field('featured_project') ) { ?>
        <article class='project featured' id="post-<?php the_ID(); ?>" <?php post_class( 'cf' ); ?> role="article">
        <a href='<?php the_permalink(); ?>'>
           <h3 class="h2"><?php the_title(); ?></h3>

           <?php    
                $url =  wp_get_attachment_url( get_post_thumbnail_id() );      
                $width = 300;                                                                  
                $height = 200;                                                                 
                $crop = true;                                                                 
                $retina = false;                                                             

                // Call the resizing function (returns an array)
                $image = matthewruddy_image_resize( $url, $width, $height, $crop, $retina ); 

           ?> 
           <img src='<?php echo $image['url']; ?>'/ alt='<?php the_title(); ?>'>
        </a>    
        <?php // the_post_thumbnail( 'projects-full', false ); ?> 

        <div class='excerpt'><?php the_excerpt(); ?></div>
        </article>
        <?php } else { ?>

is part of the code in the category file.

Thanks

Update: Still haven't found a solution, can anyone else chime in?

1
Have you tried looking at the value of get_field('featured_project')?dojs
Yea but the only way I see to get it is inside the loop, or passing the post id outside the loop.TwistedPixel88
Yes but are you sure that your if statement is working is what I am saying, you wanted get_field... to return true or 1, does it give you that?dojs
Ah ok sorry, yes im getting the expected 1 or 0 for each post.TwistedPixel88
Okay, so you have stuff printing, just not in the right order?dojs

1 Answers

0
votes

Well the problem and the solution is not on this block of code... The problem is in the query, To fix this you should query also for the meta key

<?php
$args = array_merge( $query, 
    array( 
        'meta_query' => array(
        'relation' => 'AND',
            array(
                'key' => 'featured_project',
                'value' => 1, // test if your value is a number or string
                //'type' => 'BINARY', //Maybe you should cast the meta value
                'compare' => '='
                ),
            ),
        ) 
    );
    $custom_query = new WP_Query( $args ); ?>
?

<?php if ($custom_query->have_posts()) : while ($custom_query->have_posts()) : $custom_query->the_post(); ?>
    <?php if ( get_field('featured_project') ) { ?>

Maybe you should also add the orderby on the query (something like 'orderby' => ' meta_value_num date'