簡體   English   中英

獲取帖子作者的帖子 - WordPress

[英]Get posts from post author - WordPress

我正在嘗試制作一個部分,顯示創建當前帖子的用戶創建的帖子。 我需要明顯地顯示它們,不包括當前帖子並根據某種類型的帖子(事件)。

我有這個代碼,但它不起作用。 有人能想到如何幫助我嗎?

<?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 ?>

你有幾件事。 你的$args數組中有一個非法的分號, type應該是post_type ,我也不完全確定你需要那個奇怪的比較運算符嗎?

嘗試這個:

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

另外,還要確保$post設置,這取決於你調用這個地方,可能沒有一個實例化/全球$post的對象。

這也假設您正在使用一個名為event的實際post_type ,否則如果您正在檢查元字段'_listing_type'常規帖子,您將需要刪除post_type參數,並添加一個meta_query參數 - 請注意這在大型數據庫上本質上很慢,因為postmeta表默認情況下沒有索引。 另請注意, meta_query是一個數組數組

該查詢最終看起來像這樣:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM