如何检索特定帖子类型的所有帖子标题?


9

我想以我正在回应客户端的形式在select元素内使用标题。最好的方法是什么?


问题不是很清楚。您是否要返回特定帖子类型下所有帖子的所有帖子标题?那正确吗?
艾哈迈德·福阿德

抱歉! 是的,确切的说,我已经创建了一个自定义帖子类型,创建了一些这种类型的帖子,并且我想回显带有select元素的表单,该元素允许用户选择要捐赠的标题之一(它们代表项目)。
彼得·特纳

Answers:


11

查询特定帖子类型的所有帖子标题

// Function that returns post titles from specific post type as form select element
// returns null if found no results.

function output_projects_list() {
    global $wpdb;

    $custom_post_type = 'page'; // define your custom post type slug here

    // A sql query to return all post titles
    $results = $wpdb->get_results( $wpdb->prepare( "SELECT ID, post_title FROM {$wpdb->posts} WHERE post_type = %s and post_status = 'publish'", $custom_post_type ), ARRAY_A );

    // Return null if we found no results
    if ( ! $results )
        return;

    // HTML for our select printing post titles as loop
    $output = '<select name="project" id="project">';

    foreach( $results as $index => $post ) {
        $output .= '<option value="' . $post['ID'] . '">' . $post['post_title'] . '</option>';
    }

    $output .= '</select>'; // end of select element

    // get the html
    return $output;
}

// Then in your project just call the function
// Where you want the select form to appear
echo output_projects_list();

3
对于某些事情而言,这是一个过于复杂的功能,可以使用WordPress在WP_Query类中提供的API轻松完成。@ialocin提供的答案要好得多。您不应该为此需要$ wpdb。
某处某人

是的,评论的重点是什么?:)
艾哈迈德·福阿德

1
@Brian far better其他答案是什么?从技术上讲,这是更快的,因为您仅从mysql获取所需的数据。另一个答案(更简单的答案)将所有数据获取到内存中,然后在PHP中对其进行修改。这就是PHP的更多工作。两者都是可以接受的,但是各有所长。如果您知道mysql,那么这根本不是太复杂。非常简单。
JoeMoe1984

我同意@ JoeMoe1984,此功能并不像其他答案那么简单...但是从内存角度来看,它要高效得多。SQL比(wp_list_pluck(...))是FAR更好的地方,用于减少/提取有价值的信息。是的,它稍微复杂一点,但是您的系统会为此感谢您……
mawalker

13

在我看来,您可以-使用API​​函数来获取数据。

// query for your post type
$post_type_query  = new WP_Query(  
    array (  
        'post_type'      => 'your-post-type',  
        'posts_per_page' => -1  
    )  
);   
// we need the array of posts
$posts_array      = $post_type_query->posts;   
// create a list with needed information
// the key equals the ID, the value is the post_title
$post_title_array = wp_list_pluck( $posts_array, 'post_title', 'ID' );

2
为+1 wp_list_pluck()。我总是忘了那个……
FaCE

+1向我介绍采摘!我怎么没看到这个?
Chizzle

5

对于分层帖子类型,我们具有内置功能:

wp_dropdown_pages( 
    [ 
        'post_type' => 'page',
        'echo'      => 1, 
        'name'      => 'wpse_titles', 
        'id'        => 'wpse-titles' 
    ] 
);

这将生成一个带有帖子标题帖子ID作为选项值的select元素。

例:

<select name='wpse_titles' id='wpse-titles'>
    <option class="level-0" value="1">AAA</option>
    <option class="level-0" value="2">BBB</option>
    <option class="level-1" value="3">&nbsp;&nbsp;&nbsp;CCC</option>
</select>

这不是从清除文档wp_dropdown_pages(),但它是一个包装get_pages(),并支持它的输入参数。


0

我一直做这样的事情的方式是使用get_postsforeach类似以下内容:

// Create our arguments for getting our post
$args = [
  'post_type'=>'custom-slug'
];

// we get an array of posts objects
$posts = get_posts($args);

// start our string
$str = '<select>';
// then we create an option for each post
foreach($posts as $key=>$post){
  $str .= '<option>'.$post->post_title.'</option>';
}
$str .= '</select>';
echo $str;

运作良好,谢谢。我试图将'selected'添加到当前保存的选项中,我尝试了很多不同的代码,最后来到了这一点,这允许我更新该选项,但未添加'selected'到正确的一个。关于我在做什么的任何指示?`$ str。='<选项'。($ post == $ value2?'selected = selected':'')。'>'。$ post-> post_title。'</ option>';``非常感谢您的任何建议ps对不起,我似乎没有正确添加代码,尝试了反引号
Jo_pinkish
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.