wordpress custom plugin get all post titles and permalinks

Solution:1

I use get_posts to display a dropdown with all posts in a plugin settings page:

public function posts_html()
{
    $value = get_option( 'especial_edit_post' );
    $args = array( 'numberposts' => -1 );
    $posts = get_posts( $args );

    echo "<select name='especial_edit_post' id='especial_edit_post'>";
    $selected = selected( '', $value, false );
    echo "<option value='' {$selected}>-none-</option>";

    foreach( $posts as $post )
    {
        $selected = selected( $post->ID, $value, false );
        echo "<option value='{$post->ID}' {$selected}>{$post->post_title}</option>";
    }
    echo '</select>';

}

Solution:2

We can use get_posts this way:

$args = array(
	'numberposts'	=> 20,
	'category'		=> 4
);
$my_posts = get_posts( $args );

if( ! empty( $my_posts ) ){
	$output = '<ul>';
	foreach ( $my_posts as $p ){
		$output .= '<li><a href="' . get_permalink( $p->ID ) . '">' 
		. $p->post_title . '</a></li>';
	}
	$output .= '</ul>';
}

The function above retrieves the latest 20 blog posts in the specified category (by default the 'post_type' is 'post') and returns an array of $post objects. You can iterate over the array to display the posts on the screen.