简体   繁体   中英

ACF Wordpress & sort category archive page

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?

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'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM