Support

Account

Home Forums General Issues query events by user field

Helping

query events by user field

  • Hi Folks,

    i have a problem at my wp post query for my events plugin “Events maker” by dfactory.
    at my author.php i need to show all events by a custom field query (user field).
    this filed query is called ‘vortrag_von’, $authors (in english = events host)
    its not the author from that event, its a diffrent field from my WP users ive created in acf.

    i try´d this tutorial but i failed:
    https://www.advancedcustomfields.com/resources/query-posts-custom-fields/

    here´s my query

    
    var_dump('vortrag_von', $author);
    echo $author->display_name;
    
    $args = array(
    
       'post_type' => 'event', // required
       'suppress_filters' => FALSE, // required
       'posts_per_page' => -1, // optional
       'meta_key'
    => 'vortrag_von',   // this doesnt work. no events return. without this value all events showed up
    'meta_value'         
    =>  $author          
     
       
    
    );
    
     
    
    // The Query
    $events = new WP_Query($args);
    
    // The Loop
    if ($events->have_posts()) {
       while ( $events->have_posts() ) {
           $events->the_post();
    
           
           
       
           echo '<li>' . get_the_title() . '</li>';
       
       }
    
    } else {
    
       // no events found
    
    }
    
    // Restore original Post Data
    
    wp_reset_postdata();?>
    
  • Hi @fonty87

    Please use the code tags when you paste code. Elliot has not fixed this issue yet but if you post weirdly formatted code without the code tags you’ll break the site layout making it impossible to answer you 🙂

    I’ve fixed your post now after tinkering a bit with the CSS to be able to hit the edit button 😉

    If you’re using the User field in ACF to store the author you need to query it by the user ID, not the display name.

    I think this change should do it (if that’s how you set it up)

    
    <?php
    $args = array(
    	'post_type' => 'event', // required
    	'suppress_filters' => false, // required
    	'posts_per_page' => 10000, // Should never be set to -1 as it's a risk not setting boundaries. Good code practise!
    	'meta_query' => array( // Use meta query instead
    		array(
    			'key' => 'vortrag_von',
    			'value' => $author->ID,
    			'compare' => '='
    		)
    	)
    );
    
Viewing 2 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic.