list of custom post by custom field in frontend

I’m trying to list the custom posts associated with a client.

I have a CPT “assessment”
with an ACF custom field “customer”.

The short code would go to a specific page “my assessments”

So far I was able to do the following:

function lista_mis_valoraciones_shortcode() { 
$current_user_id = get_current_user_id();
$posts = get_posts(array(
    'numberposts'   => -1,
    'post_type' => 'assessment',
    'meta_key'  => 'customer',
    'meta_value'    => $current_user_id
));


// query
$the_query = new WP_Query( $args );

if( $the_query->have_posts() ){
    while( $the_query->have_posts() ) : $the_query->the_post(); 
            <a href="<?php the_permalink(); ?>">
                <img src="<?php the_field('event_thumbnail'); ?>" />
                the_title(); 
        
    endwhile; 

endif; 
}
wp_reset_query();    // Restore global post data stomped by the_post(). 

}
add_shortcode( 'lista_mis_valoraciones', 'lista_mis_valoraciones_shortcode' );

Any kind of help is welcome.
Thank you very much in advance.

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

The main issue is that you’ve mixed up two methods of querying posts.

You’re querying the correct posts here:

$posts = get_posts(array(
    'numberposts'   => -1,
    'post_type' => 'assessment',
    'meta_key'  => 'customer',
    'meta_value'    => $current_user_id
));

But then the posts you’re actually looping through are queried here:

$the_query = new WP_Query( $args );

Where $args is undefined and has nothing to do with the previously queried posts. You can pass the same arguments to WP_Query as get_posts(), but with posts_per_page, rather than numberposts:

function lista_mis_valoraciones_shortcode() {
    $current_user_id = get_current_user_id();
    $the_query       = new WP_Query(
        array(
            'posts_per_page' => -1,
            'post_type'      => 'assessment',
            'meta_key'       => 'customer',
            'meta_value'     => $current_user_id,
        )
    );

    if ( $the_query->have_posts() ) {
        while ( $the_query->have_posts() ) : $the_query->the_post();
            ?>

            <a href="<?php the_permalink(); ?>">
            <img src="<?php the_field( 'event_thumbnail' ); ?>" />
            <?php the_title(); ?>

            <?php
        endwhile;
    }

    wp_reset_postdata();
}
add_shortcode( 'lista_mis_valoraciones', 'lista_mis_valoraciones_shortcode' );

Note the other changes I made:

  • You had mixed in HTML with PHP with the <img> and <a> tags. To output HTML you need to close the PHP tags with ?> and open them again with <?php when you’re done.
  • You had a stray endif that needed to be removed.
  • wp_reset_postdata() is more appropriate in this case than wp_reset_query().


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x