Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I am working on review plugin for my wordpress blog, and I have small problems. First this is the Plugin code:

add_action( 'init', 'create_devicemondo_review' );

function create_devicemondo_review() {
    register_post_type( 'review',
        array(
            'labels' => array(
                'name' => 'DeviceMondo Reviews',
                'singular_name' => 'DeviceMondo Review',
                'add_new' => 'Add New',
                'add_new_item' => 'Add New Product Review',
                'edit' => 'Edit',
                'edit_item' => 'Edit Product Review',
                'new_item' => 'New Product Review',
                'view' => 'View',
                'view_item' => 'View Product Review',
                'search_items' => 'Search Product Reviews',
                'not_found' => 'No Product Reviews found',
                'not_found_in_trash' => 'No Product Reviews found in Trash',
                'parent' => 'Parent Product Review'
            ),
            'public' => true,
            'menu_position' => 15,
            'supports' => array( 'title', 'editor', 'comments', 'thumbnail', 'excerpt', 'custom-fields' ),
            'taxonomies' => array( '' ),
            'menu_icon' => plugins_url( 'images/image.png', __FILE__ ),
            'rewrite' => array( 'slug' => 'review', 'with_front' => false ),
            'has_archive' => true
        )
    );
}

//Creating Meta Box Fields for Custom Post Types
add_action( 'admin_init', 'my_admin' );

function my_admin() {
    add_meta_box( 'product_review_meta_box',
        'Product Review Details',
        'display_product_review_meta_box',
        'review', 'normal', 'high'
    );
}


//Implementation of the display_product_review_meta_box Function
function display_product_review_meta_box( $review ) {
    // Retrieve current product type and product rating based on review ID
    $product_choice = esc_html( get_post_meta( $review->ID, 'product_choice', true ) );
    $product_type = esc_html( get_post_meta( $review->ID, 'product_type', true ) );
    $product_rating = intval( get_post_meta( $review->ID, 'product_rating', true ) );
    ?>
    <table>
        <tr>
            <td style="width: 100%">Editor Choice</td>
            <td><input type="text" size="80" name="product_review_choice" value="<?php echo $product_choice; ?>" /></td>
        </tr>
        <tr>
            <td style="width: 100%">Product Type</td>
            <td><input type="text" size="80" name="product_review_type" value="<?php echo $product_type; ?>" /></td>
        </tr>
        <tr>
            <td style="width: 150px">Product Rating</td>
            <td>
                <select style="width: 100px" name="product_review_rating">
                <?php
                // Generate all items of drop-down list
                for ( $rating = 5; $rating >= 1; $rating -- ) {
                ?>
                    <option value="<?php echo $rating; ?>" <?php echo selected( $rating, $product_rating ); ?>>
                    <?php echo $rating; ?> stars <?php } ?>
                </select>
            </td>
        </tr>
    </table>
    <?php
}

//Registering a Save Post Function
add_action( 'save_post', 'add_product_review_fields', 10, 3 );


//Implementation of the add_product_review_fields Function
function add_product_review_fields( $product_review_id, $product_review ) {
    // Check post type for product reviews
    if ( $product_review->post_type == 'review' ) {
        // Store data in post meta table if present in post data
        if ( isset( $_POST['product_review_choice'] ) && $_POST['product_review_choice'] != '' ) {
            update_post_meta( $product_review_id, 'product_choice', $_POST['product_review_choice'] );
        }
        if ( isset( $_POST['product_review_type'] ) && $_POST['product_review_type'] != '' ) {
            update_post_meta( $product_review_id, 'product_type', $_POST['product_review_type'] );
        }
        if ( isset( $_POST['product_review_rating'] ) && $_POST['product_review_rating'] != '' ) {
            update_post_meta( $product_review_id, 'product_rating', $_POST['product_review_rating'] );
        }
    }
}


//Register a Function to Force the Dedicated Template
add_filter( 'template_include', 'include_template_function', 1 );


//Implementation of the function
function include_template_function( $template_path ) {
    if ( get_post_type() == 'review' ) {
        if ( is_single() ) {
            // checks if the file exists in the theme first,
            // otherwise serve the file from the plugin
            if ( $theme_file = locate_template( array ( 'single-review.php' ) ) ) {
                $template_path = $theme_file;
            } else {
                $template_path = plugin_dir_path( __FILE__ ) . '/single-review.php';
            }
        }
    }
    return $template_path;
}

now the plugin works just fine and I have created a page with template single-review.php from my template folder. This page shows the list of all reviews I have published using this plugin. This is the code for that template:

<div id="primary">
    <div id="content" role="main" style="width: 605px!important;">

    <?php
    $mypost = array( 'post_type' => 'review', 'posts_per_page' => 1,);
    $loop = new WP_Query( $mypost );
    ?>
    <?php while ( $loop->have_posts() ) : $loop->the_post();?>
        <article class="brd-bottom" id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
                <!-- Display featured image in right-aligned floating div -->
                <div class="review-thumb">
                    <?php the_post_thumbnail( array( 100, 100 ) ); ?>
                </div>
                <div class="review-short-content">
                <ul>
                <li id="indv_title"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
                <li>
                <div style="float: left">
                <?php
                $nb_stars = intval( get_post_meta( get_the_ID(), 'product_rating', true ) );
                for ( $star_counter = 1; $star_counter <= 5; $star_counter++ ) {
                    if ( $star_counter <= $nb_stars ) {
                        echo '<img src="' . plugins_url( 'devicemondo-reviews/images/icon.png' ) . '" />';
                    } else {
                        echo '<img src="' . plugins_url( 'devicemondo-reviews/images/grey.png' ). '" />';
                    }
                }
                ?>
                </div>
                <?php
                $key = 'product_choice';
                $themeta = get_post_meta($post->ID, $key, TRUE);
                if($themeta != '') {
                echo '<div class="editor-pick">Editor Choice</div>';
                }
                ?></li>
                <li><?php the_terms( $post->ID, 'device_reviews_device_brand' ,  ' ' ); ?> <?php the_excerpt(); ?></li>

            </ul>
            </div>
            <!-- Display product 
review contents -->
        </article>
    <?php endwhile; ?>


    </div>
</div>

now the page which uses single-review.php template also shows all results BUT the link which I added <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a> does not shows the content on its page. It shows the list of all reviews again. I hope someone understand what I am trying to solve, and if you want I can provide you with link to test page I have.

share|improve this question
add comment

1 Answer

Refer to the template hierarchy to see how WordPress chooses a template based on the request. archive-{post_type}.php will render your list of posts without you having to query for them, and single-{post_type}.php will be used for single posts. Your issue is that you're using the single post template file as your archive view.

share|improve this answer
    
well I did tried to use it as archive too, but nothing have changed, when I click on individual post from those results, their pages, e.g. example.com/review/product-name show the list of all reviews, same as archive page instead to show the actual review content, ratings, title.... –  Boris Zegarac Dec 15 '12 at 19:51
    
also here is the actual tutorial I did followed wp.tutsplus.com/tutorials/plugins/… –  Boris Zegarac Dec 15 '12 at 19:52
    
you don't have to query for the post(s) within the template, WordPress handles the queries for you before the template is loaded, you just need to run the normal loop. The issue within your template file is that you create a new query, and never actually output the data contained in the default query. I suggest starting with your unedited single.php and archive.php template files and copying/renaming them, then editing them for your custom post type by adding extra meta output, etc.. –  Milo Dec 15 '12 at 20:04
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.