Get title from wordpress custom post type

2019-08-27 20:57发布

I have a custom post type like below (not customized yet).

function movie_reviews_init() {

    $args = array(
      'label' => 'Movie Reviews',
        'public' => true,
        'show_ui' => true,
        'capability_type' => 'post',
        'hierarchical' => false,
        'rewrite' => array('slug' => 'movie-reviews'),
        'query_var' => true,
        'menu_icon' => 'dashicons-video-alt',
        'show_in_menu' => 'myplugin',
        'supports' => array(
            'title',
            'editor',
            'page-attributes',)
        );
    register_post_type( 'movie-reviews', $args );
}

I have looked and looked but cant figure out how to get the title from the posts. I need the title in a variable to compare with the users input using levenshtein-distance and later display the most relevant post.

标签: php wordpress
1条回答
霸刀☆藐视天下
2楼-- · 2019-08-27 21:11

In My plug-in custom post type Register

function register_cpt_gallery() {
        $labels = array(
            'name' => _x( 'Gallery', 'gallery' ),
            'singular_name' => _x( 'gallery', 'gallery' ),
            'add_new' => _x( 'Add New Album', 'gallery' ),
            'add_new_item' => _x( 'Add New Album', 'gallery' ),
            'edit_item' => _x( 'Edit gallery', 'gallery' ),
            'new_item' => _x( 'New Album', 'gallery' ),
            'view_item' => _x( 'View Album', 'gallery' ),
            'search_items' => _x( 'Search Album', 'gallery' ),
            'not_found' => _x( 'No Album found', 'gallery' ),
            'not_found_in_trash' => _x( 'No Album found in Trash', 'gallery' ),
            'parent_item_colon' => _x( 'Parent Album:', 'gallery' ),
            'menu_name' => _x( 'Gallery', 'gallery' ),
        ); 
        $args = array(
            'labels' => $labels,
            'hierarchical' => true,
            'description' => 'Gallery filterable by genre',
            'supports' => array( 'title', 'editor', '', 'thumbnail', '', '', '', '', '' ),
            'taxonomies' => array( 'genres' ),
            'public' => true,
            'show_ui' => true,
            'show_in_menu' => true,
            'menu_position' => 5,
            'menu_icon' => 'dashicons-images-alt',
            'show_in_nav_menus' => true,
            'publicly_queryable' => true,
            'exclude_from_search' => false,
            'has_archive' => true,
            'query_var' => true,
            'can_export' => true,
            'rewrite' => true,
            'capability_type' => 'post'
        ); 
        register_post_type( 'gallery', $args );
    }
    add_action( 'init', 'register_cpt_gallery' );

Get Post title in php page

<?php $gallery_args = array(
                'posts_per_page'   => -1,
                'orderby'=> 'date',
                'order'=> 'DESC',
                'post_type'=> 'gallery',
                'post_status'=> 'publish',
                'suppress_filters' => true 
        );
    $posts_display_gallery = get_posts( $gallery_args ); 
    foreach($posts_display_gallery as $rows){
        $post_title = $rows->post_title;
    } ?>
查看更多
登录 后发表回答