Support

Account

Home Forums Front-end Issues Used ACF to Add Category to Page, Can't use ARG to get posts that match page cat

Unread

Used ACF to Add Category to Page, Can't use ARG to get posts that match page cat

  • I used ACF to add categories to my pages so that I could get posts the match the page’s category in the page template. However, I have been unable to get this to work. The below works for hard using the category:

    <ul>
    <?php 
    $args = array( 'posts_per_page' => 3, 'category_name' => 'senate');
    
    $myposts = get_posts( $args );
    foreach ( $myposts as $post ) : setup_postdata( $post ); ?>
    <li>
        <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
    </li>
    <?php endforeach; 
    wp_reset_postdata();?>
    
    </ul>

    But when I try to use the taxonomy field:

    <?php $args = array( 'posts_per_page' => 3, 'category_name' => get_field('post_sorting'));
    
    $myposts = get_posts( $args );
    foreach ( $myposts as $post ) : setup_postdata( $post ); ?>
    <li>
        <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
    </li>
    <?php endforeach; 
    wp_reset_postdata();?>

    It gets all posts. Even when I’ve done $sort = get_field('post_sorting'); and used that in category_name=>$sort it grabs all posts. Is there any way I can set it up so that the category I set in post_sorting for the page is used as the category of posts that are gathered?

Viewing 1 post (of 1 total)

The topic ‘Used ACF to Add Category to Page, Can't use ARG to get posts that match page cat’ is closed to new replies.