Use ACF field in a WP_Query meta_query

69 Views Asked by At

I want to show posts from a CPT, created by advanced custom fields where the custom field value is the same as the currently logged in username.

How would I go about this?

I tried it with the functions.php below, implementing it with a shortcode on the frontend. However it keeps returning a blank paragraph.

Does anyone know how to do it? It would tremendously help me!

function facturen_shortcode($atts) {
    $current_user = wp_get_current_user();
    $args = array(
        'post_type' => 'factuur',
        'meta_query' => array(
            array(
                'key' => 'field_65bcf1c7d79ca',
                'value' => $current_user->user_login,
                'compare' => '=',
            ),
        ),
    );
    $query = new WP_Query($args);

    ob_start();

    if ($query->have_posts()) {
        while ($query->have_posts()) {
            $query->the_post();
            // Toon hier je berichten van het aangepaste berichttype
            the_title();
            echo '<br>';
        }
    } else {
        // Geen berichten gevonden
    }

    wp_reset_postdata();

    return ob_get_clean();
}
add_shortcode('facturen', 'facturen_shortcode');

I tried creating it with a functions.php function and shortcode [facturen], but it just gives me a blank paragraph.

0

There are 0 best solutions below