Skip to content

Instantly share code, notes, and snippets.

@alenabdula
Last active August 29, 2015 14:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alenabdula/d0a942f0536c793a8bad to your computer and use it in GitHub Desktop.
Save alenabdula/d0a942f0536c793a8bad to your computer and use it in GitHub Desktop.
<?php
/**
* Reviews Custom Post Type
*/
function reviews_custom_post_type() {
# Custom Labels
$labels = array(
'name' => 'Reviews',
'singular_name' => 'Review',
'menu_name' => 'Reviews',
'parent_item_colon' => 'Parent Review:',
'all_items' => 'All Reviews',
'view_item' => 'View Review',
'add_new_item' => 'Add New Review',
'add_new' => 'Add New',
'edit_item' => 'Edit Review',
'update_item' => 'Update Review',
'search_items' => 'Search Review',
'not_found' => 'Not found',
'not_found_in_trash' => 'Not found in Trash',
);
# Custom Arguments
$args = array(
'label' => 'reviews',
'description' => 'Reviews Description',
'labels' => $labels,
'supports' => array( 'title', 'editor', 'excerpt',
'author', 'thumbnail', 'comments',
'trackbacks', 'revisions', 'custom-fields',
'page-attributes', 'post-formats', ),
'taxonomies' => array( 'category', 'post_tag' ),
'hierarchical' => false,
'public' => true,
'show_ui' => true,
'show_in_menu' => true,
'show_in_nav_menus' => true,
'show_in_admin_bar' => true,
'menu_position' => 5,
'can_export' => true,
'has_archive' => true,
'exclude_from_search' => false,
'publicly_queryable' => true,
'capability_type' => 'page',
);
# Register Custom Post Type 'reviews'
register_post_type( 'reviews', $args );
}
# Hook into the 'init' action
add_action( 'init', 'reviews_custom_post_type', 0 );
  1. Open functions.php and add following code.
  2. Now log into WP Admin and you should see "Reviews" as one of the menu items. Add few entries, add title, feature image and content. See Image 1 Image 2
  3. Now to request the data created, we can create a template or use WP_Query. In this case I decided to use WP_Query, code. You can insert this code in whatever template you're trying to modify.
  4. Lets add some Meta Data by Installing ACF
  5. Once installed you'll see "Custom Fields" menu item show up. Image .
  6. Add Meta data. Image 1 Image 2 .
  7. Scroll Down... Next you need to associate this Data with the Custom Post Type of 'reviews' we created earlier in step #1. Image
  8. Next go to Reviews and edit one of the entries. You'll see additional Meta Data field now associated with that post. Edit it and hit Update. Image
<?php
$query_arguments = array('post_type' => 'reviews', 'orderby' => 'id');
$reviews_query = new WP_Query( $query_arguments );
# WordPress Loop
if ( $reviews_query->have_posts() ) {
while ( $reviews_query->have_posts() ) {
$reviews_query->the_post(); ?>
<article>
<h1><?php the_title(); ?></h1>
<?php the_post_thumbnail(array(100,100)); ?>
<?php the_content(); ?>
<small>Rating: <?php the_field('review_rating'); ?></small>
<hr>
</article>
<?php } } else { echo '<h2>Nothing Found</h2>'}
wp_reset_postdata();
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment