• Resolved WoutCompay

    (@woutcompay)


    I would like to display posts from a certain category on the homepage based on which category is selected by the user in an Advanced custom field select.

    I now have the below working code, which shows posts from category ‘Lisbon’, but I would like to replace ‘new WP_Query( ‘category_name=lisbon’ )’ by something like ‘new WP_Query( ‘category_name=[specific advanced custom field value]’ )’

    How would I go about doing this? Thanks very much in advance!


    <?php
    // the query
    $the_query = new WP_Query( 'category_name=lisbon' ); ?>

    <?php if ( $the_query->have_posts() ) : ?>

    <!-- pagination here -->

    <!-- the loop -->
    <?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>

    <h1><?php the_title(); ?></h1>

    <?php endwhile; ?>
    <!-- end of the loop -->

    <!-- pagination here -->

    <?php wp_reset_postdata(); ?>

    <?php else : ?>
    <p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
    <?php endif; ?>

Viewing 2 replies - 1 through 2 (of 2 total)
  • Depending on the custom field you have created, it either stores the category id or the category slug. You need to know which, and also the name of the field.
    If for example the field name is my_category and it stores the id you would then do:

    $the_query = new WP_Query( array(
        'cat' => get_field('my_category')
    ) );

    If 'my_category' holds the slug, just replace 'cat' with 'category_name'

    Thread Starter WoutCompay

    (@woutcompay)

    Thank you very much Anastis, that solved it!

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Advanced Custom Fields value in WP_Query’ is closed to new replies.