简体   繁体   中英

Get posts from post author - WordPress

I'm trying to make a section that shows the posts created by the user that created the current post. I need to show them obviously excluding the current post and according to a certain type of post (event).

I have this code, but it is not working. Can someone think of how to help me?

<?php 

    $args = array(
    'author'        =>  $post->post_author;
    'type'          => $post_meta['_listing_type'][0] == 'event',
    'order'         =>  'ASC',
    'posts_per_page' => -1
    );

    $eventos = get_posts( $args );

    foreach ( $eventos as $evento ) {
        $output .= '<div>'.$evento.'</div>';
    endforeach;

    if(!empty($eventos)) : ?> 

        <div id="listing-eventosartista" class="listing-section">
            <h3 class="listing-desc-headline margin-top-60 margin-bottom-30"><?php esc_html_e('Eventos del artista','listeo_core'); ?></h3>
                <div class="single-item">
                    <?php echo $output; ?>
                </div>

        </div>
    <?php endif ?>

You've got a couple of things. You've got an illegal semi-colon in your $args array, type should be post_type , I'm also not entirely sure you need that weird comparison operator in there?

Try this:

$args = array(
    'author'         => $post->post_author,
    'post_type'      => 'event',
    'order'          => 'ASC',
    'posts_per_page' => -1
);

Also make sure that $post is set, depending on where you're calling this, there may not be an instantiated/global $post object.

This is also assuming you're using an actual post_type called event , otherwise if you're checking regular posts for the meta field '_listing_type' , you'll need to drop the post_type argument, and add a meta_query argument instead - note that this is inherently slow on large databases because the postmeta table isn't indexed by default. Also note that meta_query is an array of arrays .

That query would end up looking something like this:

$args = array(
    'author'         => $post->post_author,
    'order'          => 'ASC',
    'posts_per_page' => -1,
    'meta_query'     => array(
        array(
            'meta_key'   => '_listing_type',
            'meta_value' => 'event'
        )
    )
);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM