0

I have created a Custom Post Type and added 50+ posts. Now this post type have some meta values which I need to display.

Here is how I created the post type:

function colorshadescpt() {
    register_post_type( 'colorshades',
        array(
            'labels' => array(
            'name' => __('Color Shades'),
            'singular_name' => __('Shade'),
            'add_new_item' => __('Add New'),
            'edit_item' => __('Edit'),
            'new_item' => __('Add New'),
            'view_item' => __('View'),
        ),
        'public' => true,
        'supports' => array( 'title'),
        'capability_type' => 'post'
        )
    );
}
add_action('init', 'colorshadescpt');

And added this meta_boxes:

$prefix = 'cg_';

$meta_boxes = array();

$meta_boxes[] = array(
    'id' => 'colorcodes',
    'title' => 'Color Code',
    'pages' => array('colorshades'),
    'fields' => array(
        array(
            'name' => 'Color code',                 // field name
            'desc' => 'Enter Color code here.', // field description, optional
            'id' => 'color_code',               // field id, i.e. the meta key
            'type' => 'text',                       // text box
            'std' => '',                    // default value, optional
        ),
    )
);



$meta_boxes[] = array(
    'id' => 'colorshades',
    'title' => 'Color Value',
    'pages' => array('colorshades'),
    'fields' => array(
        array(
            'name' => 'R',                  // field name
            'desc' => 'Enter Red color value here.',    // field description, optional
            'id' => 'red_code',             // field id, i.e. the meta key
            'type' => 'text',                       // text box
            'std' => '',                    // default value, optional
        ),
        array(
            'name' => 'G',                  // field name
            'desc' => 'Enter green color value here.',  // field description, optional
            'id' => 'green_code',               // field id, i.e. the meta key
            'type' => 'text',                       // text box
            'std' => '',                    // default value, optional
        ),
        array(
            'name' => 'B',                  // field name
            'desc' => 'Enter blue color value here.',   // field description, optional
            'id' => 'blue_code',                // field id, i.e. the meta key
            'type' => 'text',                       // text box
            'std' => '',                    // default value, optional
        ),
        array(
            'name' => 'Type',                   // field name
            'desc' => 'Please Select type for shade.',  // field description, optional
            'id' => 'shade_color_type',             // field id, i.e. the meta key
            'type' => 'radio',                      // text box
            'std' => 'product',
            'options' => array(                     // array of key => value pairs for radio options
                'product' => 'Product',
                'machine' => 'Machine',
            ),                  // default value, optional
        ),
    )
);



$meta_boxes[] = array(
    'id' => 'productid',
    'title' => 'Product Settings',
    'pages' => array('colorshades'),
    'fields' => array(
        array(
            'name' => 'Product Id',
            'desc' => 'Enter the product id here.',
            'id' => 'colors_product_id',
            'type' => 'text',
            'std' => '',
        ),
    )
);


$meta_boxes[] = array(
    'id' => 'shade_details',
    'title' => 'Other Details',
    'pages' => array('colorshades'),
    'fields' => array(
        array(
            'name' => 'Details',                    // field name
            'desc' => 'Enter shade details.',   // field description, optional
            'id' => 'colors_details',               // field id, i.e. the meta key
            'type' => 'textarea',                       // text box
            'std' => '',                    // default value, optional
        ),
    )
);


foreach ($meta_boxes as $meta_box) {
    $my_box = new RW_Meta_Box_Taxonomy($meta_box);
}

Here is the screenshot of how it looks as a post: enter image description here

How I am getting the result on the other side:

<?php

global $product;
 
  $args = array(
    'post_type' => 'colorshades',
    'fields' => 'ids',
    'posts_per_page' => -1,
    'meta_query' => array(
      array(
        'key'     => 'colors_product_id',
        'value'   => $product->id,
        'compare' => '=',
      )
    )
  );

$query = new WP_Query( $args );

if ($query->have_posts()):
foreach( $query->posts as $id ):
$Color_Code = get_post_meta($id, "color_code", true);
$R = get_post_meta($id, "red_code", true);
$G = get_post_meta($id, "green_code", true);
$B = get_post_meta($id, "blue_code", true);
$all_color_product_notification = '';
$image_url = site_url().'/wp-content/themes/porto/images/swatch.png';
echo '<div class="colors_box mb-4 mx-lg-4 mx-md-3 mx-2">
<a href="javascript:;" class="colorshade" style="background:rgb('.$R.','.$G.','.$B.');" ><img src="'.$image_url.'" class="img-responsive" style="width:100%; height:70px;"></a><p> '.get_the_title( $id ).'</p>
</div>';
?>

<?php
endforeach;
endif;

As you can see I am getting the post based on the meta field "product id", but Meta Query returns empty.

Without the meta query I get all the posts but I want on basis of Ids.

Here is the result if I query without Meta values:

enter image description here

Please guide me if I am doing something wrong here.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.