Wp Meta Query does not work while simple Query Works - php

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:
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">
<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:
Please guide me if I am doing something wrong here.

Related

CMB2 repeatable file_list type

I am searching everywhere for an example of how to populate/access images entered inside of repeatable file-list group in CMB2. All the examples I could find is only for a single image: here is the link https://github.com/CMB2/CMB2/wiki/Field-Types#group
MY CODE:
// creating a group
$group_field_id = $bautage->add_field( array(
id' => 'tagen_entries',
'type' => 'group',
'description' => __( 'Tagen', 'cmb2' ),
'repeatable' => true,
'options' => array(
'group_title' => __( 'Tagen {#}', 'cmb2' ), // since version 1.1.4, {#} gets replaced by row number
'add_button' => __( 'Add Another Entry', 'cmb2' ),
'remove_button' => __( 'Remove Entry', 'cmb2' ),
'sortable' => true,
),
) );
// creating repeatable fields
$bautage->add_group_field( $group_field_id, array(
'name' => 'Tag Name',
'id' => 'tag_name',
'type' => 'text',
) );
$bautage->add_group_field( $group_field_id, array(
'name' => 'Tage Photos',
'id' => 'tage_photos',
'type' => 'file_list',
) );
// page code
$tagen_entries = get_post_meta( get_the_ID(), 'tagen_entries', true );
foreach ( (array) $tagen_entries as $key => $entry ) {
if ( isset( $entry['tag_name'] ) ) {
$tag_name = esc_html( $entry['tag_name'] );
echo $tag_name;
}
if ( isset( $entry['tage_bildern'] ) ) {
// loop to bring all the photos
// also need the first photo from each entry to be used as a click trigger to open the slideshow
}
}

How to add acf form post categry field in acf form front end but category dropdown not showing

Category dropdown not showing on front end.Please suggest or let me know where i am wrong in my code.
<?php acf_form(array(
'post_id' => 'new_post',
'post_title' => true,
'post_content' => true,
'post_category' => true,
'field_groups' => array('group_57d9928ba5858'),
'new_post' => array(
'post_type' => 'festival',
'post_status' => 'draft'
),
'submit_value' => 'Submit Post',
'updated_message' => 'Saved!',
'uploader' => 'wp',
));?>
I can't see post_category as an option in https://www.advancedcustomfields.com/resources/acf_form/ I think it should be in
'new_post' => array(
'post_type' => 'foo',
'post_status' => 'publish',
'post_category' => array ('bar')
),
ACF doesn't automatically add the category option to front-end forms. The best solution is to setup a custom field referencing the category taxonomy (Taxonomy Field Type) and setting the Add Term, Save Term & Load Term values to true so the field acts like a native category field.
Alternatively, you can add the following code to your functions file that was generated from the ACF Generate PHP Tool for a field I set up with these settings.
if( function_exists('acf_add_local_field_group') ) {
acf_add_local_field_group(array(
'key' => 'group_5dc382838casdf',
'title' => 'Category',
'fields' => array(
array(
'key' => 'field_5ddcdd3232asD',
'label' => 'Category',
'name' => 'category',
'type' => 'taxonomy',
'instructions' => '',
'required' => 1,
'conditional_logic' => 0,
'wrapper' => array(
'width' => '',
'class' => '',
'id' => '',
),
'taxonomy' => 'category',
// Accepts SINGLE VALUE: radio, select, MULTIPLE VALUES: multi_select, checkbox
'field_type' => 'multi-select',
'allow_null' => 0,
// Ensures taxonomy relationships are set on add, save, load
'add_term' => 1,
'save_terms' => 1,
'load_terms' => 1,
'return_format' => 'id',
'multiple' => 0,
// Form Placeholder
'placeholder' => 'Select Topics'
)
),
'location' => array(
array(
array(
'param' => 'post_type',
'operator' => '==',
// ADD YOUR POST-TYPE HERE
'value' => 'post',
),
),
),
'menu_order' => 0,
'position' => 'normal',
'style' => 'default',
'label_placement' => 'top',
'instruction_placement' => 'label',
'hide_on_screen' => '',
'active' => true,
'description' => '',
));
}
This is working...
function acf_xyz_categories( $value, $post_id, $field ){
if($value != ''){
$valueint = array_map('intval', $value);
$set_taxonomy_ids = wp_set_object_terms( $post_id, $valueint, 'your_taxonomy_name', true );
}
return $value;
}
add_filter('acf/update_value/name=abc_category', 'acf_xyz_categories', 10, 3);

Order wp_query by Vafpress meta

I'm trying to order a custom-post-type WP_Query by a custom vafpress metabox:
My Metabox
$mb = new VP_Metabox(array(
'id' => 'meta_person',
'types' => array('person'),
'title' => __('Person informations', 'dr_domain'),
'priority' => 'high',
'template' => array(
array(
'type' => 'textbox',
'name' => 'person_name',
'label' => __('Your Name', 'vp_textdomain'),
),
array(
'type' => 'textbox',
'name' => 'person_city',
'label' => __('Your City', 'vp_textdomain'),
),
),
));
I want to retrieve Persons by their city, for that here is my Query:
$args = array(
'post_type' => 'person',
'meta_query'=> array(
array(
'key' => 'meta_person.person_city',
'compare' => 'LIKE',
'value' => 'Paris',
),
),
);
The WP_Query is not returning any result. I tried this option too, with no results:
'meta_key' => 'person_city' // No Result
So How can I use WP_Query with field and without metabox ID.
Thank you.

Displaying the latest posts from a custom field group

i have this custom Metabox field group from https://github.com/WebDevStudios/Custom-Metaboxes-and-Fields-for-WordPress/wiki/Field-Types#group containing 3 fields and displayed on the Products page template, i want to call the latest posts the client add to display them on my front-page. here is the code of the custom fields.
/**
* Repeatable Field Groups
*/
$meta_boxes['field_group'] = array(
'id' => 'field_group',
'title' => __( 'Manage your products here', 'cmb2' ),
'show_on' => array( 'id' => array( 11, ) ), // Specific post IDs to display this metabox
'object_types' => array( 'page', ),
'fields' => array(
array(
'id' => $prefix . 'repeat_group',
'type' => 'group',
'options' => array(
'group_title' => __( 'Product {#}', 'cmb2' ), // {#} gets replaced by row number
'add_button' => __( 'Add Another Product', 'cmb2' ),
'remove_button' => __( 'Remove Product', 'cmb2' ),
'sortable' => true, // beta
),
'fields' => array(
array(
'name' => 'Product Name',
'id' => 'product_name',
'type' => 'text',
),
array(
'name' => 'Product Description',
'description' => 'Write a short description for this Product',
'id' => 'product_description',
'type' => 'textarea',
),
array(
'name' => 'Product Image',
'id' => 'product_image',
'type' => 'file',
),
),
),
),
);
UPDATE:
i found this http://codex.wordpress.org/Function_Reference/wp_get_recent_posts but i still can't figure out how to make it get the posts from the custom fields

select box in wordpress for meta field

I am registering a post in my wp-admin but I don't want editor etc, so adding some field. By the R & D I founded how to add text box and it's awesome but now I have to add a select box and the option value should be post title. I don't want to do this by plugin.
I added text field as:
$client_meta_box = array(
'id' => 'meta-client',
'title' => __('Client Options','mfn-opts'),
'page' => 'client',
'context' => 'normal',
'priority' => 'high',
'fields' => array(
array(
'id' => 'post-link',
'type' => 'text',
'title' => __('Link', 'opts'),
'sub_desc' => __('Link to client`s site', 'opts'),
),
),
);
and I can add select box by just change the type as 'type' => 'select' but how did I get the post title value in option.
Using this to add meta box lile text, chackbox, selectoption.
$meta_boxes[] = array(
'id' => 'meta-client', // meta box id, unique per meta box
'title' => 'Client Options', // meta box title
'pages' => array('client'), // post types, accept custom post types as well,
//default is array('post'); optional
'priority' => 'high', // order of meta box: high (default), low; optional
'fields' => array(
array(
'label'=> 'Text Input',
'desc' => 'A description for the field.',
'id' => $prefix.'text',
'type' => 'text'
),
array(
'label'=> 'Textarea',
'desc' => 'A description for the field.',
'id' => $prefix.'textarea',
'type' => 'textarea'
),
array(
'label'=> 'Checkbox Input',
'desc' => 'A description for the field.',
'id' => $prefix.'checkbox',
'type' => 'checkbox'
),
array(
'label'=> 'Select Box',
'desc' => 'A description for the field.',
'id' => $prefix.'select',
'type' => 'select',
'options' => array(
'option1' => 'Optionone', // 'value'=>'label'
'option2' => 'Optiontwo',
'option3' => 'Optionthree'
)
)
);

Categories