To display all the categories name with checkbox from custom taxonomy called ct_category I have the following code:
$terms = get_terms('ct_category');
foreach ( $terms as $term ) {
echo '<li class='.$term->term_id.'><label > <input class="taxonomy_category" type="checkbox" name="taxonomy_category['.$term->term_id.']" value ="'.$term->term_id.'" />'.$term->name.'</label ></li>';
}
I would like to display content only from checked categories. I tried following that didn't work:
$args = array(
'post_type' => 'cpt',
'tax_query' => array(
array(
'taxonomy' => $ct_category,
'field' => 'term_id',
'terms' => $_POST['taxonomy_category']
)
)
);
$loop = new WP_Query( $args );
I can guess that the issue is in 'terms' => $_POST['taxonomy_category']. If name attribute taxonomy_category['.$term->term_id.'] can be displayed as array in 'terms' =>, the issue would be fixed. Spent plenty of times searching google but couldn't find any solution.
Here is the full code
<?php
function add_meta_box() {
add_meta_box( 'ct_metabox', 'CT', 'meta_box_content_output', 'cpt', 'normal' );
}
add_action( 'add_meta_boxes', 'add_meta_box' );
function meta_box_content_output ( $post ) {
wp_nonce_field( 'save_meta_box_data', 'meta_box_nonce' );
$taxonomy_category = get_post_meta( $post->ID, 'taxonomy_category', true );
function categories_checkbox(){
$terms = get_terms('ct_category');
foreach ( $terms as $term ) {
echo '<li class='.$term->term_id.'><label > <input class="taxonomy_category" type="checkbox" name="taxonomy_category['.$term->term_id.']" value ="'.$term->term_id.'" />'.$term->name.'</label ></li>';
}
}
<?
<ul>
<li>
<?php categories_checkbox() ?>
<li>
</ul>
<?php
}
function save_meta_box_data( $post_id ) {
// Check if our nonce is set.
if ( ! isset( $_POST['meta_box_nonce'] ) ) {
return;
}
// Verify that the nonce is valid.
if ( ! wp_verify_nonce( $_POST['meta_box_nonce'], 'save_meta_box_data' ) ) {
return;
}
// If this is an autosave, our form has not been submitted, so we don't want to do anything.
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
return;
}
// Check the user's permissions.
if ( isset( $_POST['post_type'] ) && 'page' == $_POST['post_type'] ) {
if ( ! current_user_can( 'edit_page', $post_id ) ) {
return;
}
} else {
if ( ! current_user_can( 'edit_post', $post_id ) ) {
return;
}
}
$taxonomy_category_value = "";
if(isset($_POST["logo_taxonomy_category"])) {
$taxonomy_category_value = sanitize_text_field( $_POST["logo_taxonomy_category"] );
}
update_post_meta($post_id, "logo_taxonomy_category", $taxonomy_category_value);
}
add_action( 'save_post', 'save_meta_box_data' );
?>
<?php
$args = array(
'post_type' => 'cpt',
'tax_query' => array(
array(
'taxonomy' => $ct_category,
'field' => 'term_id',
'terms' => $taxonomy_category
)
)
);
$loop = new WP_Query( $args );
?>
Print the checkboxes as this
echo '<li class='.$term->term_id.'><label > <input class="taxonomy_category" type="checkbox" name="taxonomy_category[]" value ="'.$term->term_id.'" />'.$term->name.'</label ></li>';
This will collect all the IDs in an array $_POST['taxonomy_category']
And then write the wp_query as below:
remove $ from taxonomy name and pass the value of terms as below
$args = array(
'post_type' => 'cpt',
'tax_query' => array(
array(
'taxonomy' => 'ct_category',
'field' => 'term_id',
'terms' => $_POST['taxonomy_category'],
'operator'=> 'IN'
)
)
);
$loop = new WP_Query( $args );
Related
I have done all the child category shows in each parent category. Now I want the current or active category not to be displayed when I am on the category page.
$queried_object = get_queried_object();
$child_terms = get_term_children ( $queried_object->term_id, 'product_cat' );
$main_term = (is_wp_error($child_terms) || empty($child_terms)) ? get_term ( $queried_object->parent, 'product_cat' ) : $queried_object;
$terms_arg = get_terms( 'product_cat', array(
'orderby' => 'name',
'hide_empty' => false,
'parent' => $main_term->term_id,
) );
if( !empty( $terms_arg ) && !is_wp_error( $terms_arg ) ){
foreach( $terms_arg as $display_term ){
printf(
'<div class="cat-list"><h3%s>%s</h3></div>',
($display_term->term_id == $queried_object->term_id) ? : '',
esc_url(get_term_link($display_term->term_id)),
$display_term->name,
);
}
}
try this
foreach( $terms_arg as $display_term ){
if( $display_term->term_id != $queried_object->term_id ) {
printf(
'<div class="cat-list"><h3>%s</h3></div>',
esc_url(get_term_link($display_term->term_id)),
$display_term->name,
);
}
}
According to me get_terms() is having the options of exclude under the argument parameter
Try with below code
$terms_arg = get_terms( 'product_cat', array(
'orderby' => 'name',
'hide_empty' => false,
'parent' => $main_term->term_id,
'exclude' => $display_term->term_id, //assuming that $display_term->term_id is getting current category id
) );
I am trying to display posts of specific category(taxonomy) that is 'Book1'.
I tried to display it with the following code.
$args = array(
'post_type' => 'book',
'posts_per_page' => 6,
'tax_query' => array(
array(
'taxonomy' => 'Book1',
'field' => 'id',
'terms' => 1
)
)
);
echo '<br>';
$postss = get_posts( $args );
if ( ! empty( $postss ) && is_array( $postss ) ) {
// Run a loop and print them all
$i=1;
foreach ( $postss as $termm ) {
echo ' '.$i.' '.$termm->post_title. '<br>';
$i++;
}
}
?>
In output no any item is displayed.
$custom_terms = get_terms('Book1');
foreach($custom_terms as $custom_term) {
wp_reset_query();
$args = array(
'post_type' => 'book',
'posts_per_page' => 6,
'tax_query' => array(
array(
'taxonomy' => 'Book1',
'field' => 'slug',
'terms' => $custom_term->slug,
),
),
);
$loop = new WP_Query($args);
if($loop->have_posts()) {
echo '<h2>'.$custom_term->name.'</h2>';
while($loop->have_posts()) : $loop->the_post();
echo ''.get_the_title().'<br>';
endwhile;
}
}
try this code
$args = array(
'post_type' => 'book',
'posts_per_page' => 6,
'tax_query' => array(
array(
'taxonomy' => 'Book1',
'field' => ''term_id', // here you are worng name too
'terms' => 1
)
)
);
echo '<br>';
$postss = get_posts( $args );
if ( ! empty( $postss ) && is_array( $postss ) ) {
// Run a loop and print them all
$i=1;
foreach ( $postss as $termm ) {
echo ' '.$i.' '.$termm->post_title. '<br>';
$i++;
}
}
?>
// best solution
<?php
$query = new WP_Query( array(
'post_type' => 'book', // name of post type.
'tax_query' => array(
array(
'taxonomy' => 'Book1', // taxonomy name
'field' => 'term_id', // term_id, slug or name
'terms' => 1, // term id, term slug or term name
)
)
) );
while ( $query->have_posts() ) : $query->the_post();
// do stuff here....
endwhile;
/**
* reset the orignal query
* we should use this to reset wp_query
*/
wp_reset_query();
?>
I did it by doing like this. Thanks all.
$args = array(
'post_type' => 'book',
'cat' => '35'
);
echo '<br>';
$postss = query_posts($args);
if ( ! empty( $postss ) && is_array( $postss ) ) {
// Run a loop and print them all
?><?php $i=1;
foreach ( $postss as $termm ) { ?>
<?php echo ' '.$i.' '.$termm->post_title. '<br>';$i++;?>
<?php
}
}
I am trying to do an insert to a MySQL database in WordPress using $wpdb->insert. It is just a simple log, but it is not working.
I am trying to save some basic items from an order I received after a purchase made in WooCommerce. The page to which I added the code is order-details.php and belongs to WooCommerce.
The code:
<?php
do_action( 'woocommerce_order_details_before_order_table_items', $order );
$fecha = date('Y-m-d H:i:s');
$creditos =0;
foreach ( $order_items as $item_id => $item ) {
$item_quantity = $item->get_quantity();
$product = $item->get_product();
$product_name = $product->get_name();
if( $product_name == "orián (paquete mínimo)"){
$creditos=15;
}
if( $product_name == "Andrómeda (paquete medio)"){
$creditos=30;
}
if( $product_name == "Lira (paquete básico)"){
$creditos=50;
}
if( $product_name == "Cisne (paquete real + 15 créditos)"){
$creditos=65;
}
if( $product_name == "Perseo (Paquete Premium + 30 créditos)"){
$creditos=120;
}
echo $item_quantity." ".$product_name." ".$creditos." ".$fecha." ".get_current_user_id();
$wpdb->insert(
'registrorden',
array(
'idusuario' => get_current_user_id(),
'paquete' => $product_name,
'cantidad' => $item_quantity,
'creditos' => $creditos
),
array(
'%d',
'%s',
'%d',
'%d'
)
);
wc_get_template( 'order/order-details-item.php', array(
'order' => $order,
'item_id' => $item_id,
'item' => $item,
'show_purchase_note' => $show_purchase_note,
'purchase_note' => $product ? $product->get_purchase_note() : '',
'product' => $product,
) );
}
do_action( 'woocommerce_order_details_after_order_table_items', $order );
?>
The code I added:
$wpdb->insert(
'registrorden',
array(
'idusuario' => get_current_user_id(),
'paquete' => $product_name,
'cantidad' => $item_quantity,
'creditos' => $creditos
),
array(
'%d',
'%s',
'%d',
'%d'
)
);
Here is a snapshot of the table, and here is the error.
What can I do to fix this?
I want to get similar (related) posts that should be matching atleast 3 tags (all post have more then 10 tags). Is this possible? if yes please help. I have no idea about it.
<?php
$rTag = wp_get_post_tags($post->ID);
foreach($rTag as $individual_tag) $tag_ids[] = $individual_tag->term_id;
$related = get_posts( array(
'tax_query' => array(
array(
'taxonomy' => 'post_tag',
'field' => 'term_id',
'terms' => $tag_ids,
),
),
'numberposts' => -1,
'post__not_in' => array($post->ID),
) );
?>
<section id="discover">
<h2>Related posts</h2>
<div class="singlepost">
<?php if( $related ) foreach( $related as $post ) {
setup_postdata($post);
$tag_count = 0;
$tag_min_match = 3;
foreach ( $tag_ids as $tag ) {
if ( has_tag( $tag ) && $tag_count < $tag_min_match ) {
$tag_count ++;
}
}
if ($tag_count >= $tag_min_match) {
echo '<h4>'. get_the_title() .'</h4>';
}
}
wp_reset_query(); ?>
</div>
</section>
There are 860+ products with different variations. The task is to allow user bulk edit "used for variations" field for some attributes. For example, we have size, gender that is still needed for variations, but also we have material, that is needed for filters only, so deleting this attribute is not a solution.
Totally untested and not particularly ideal if you have a lot of products as it could time out, but theoretically you could loop through all the variable products and update the _product_attributes meta value. I highly suggest you have a data backup and/or do this is a staging site.
function so_41978670_run_once(){
$args = array(
'post_type' => 'product',
'posts_per_page' => -1,
'tax_query' => array(
array(
'taxonomy' => 'post_type',
'field' => 'slug',
'terms' => 'variable',
),
),
);
$products = new WP_Query( $args );
if( $products->have_posts() ){
while ( $products->have_posts() ) {
$products->the_post();
$product_id = get_the_ID();
$product = wc_get_product( $product_id );
$attributes = $product->get_attributes();
$meta_values = array();
if ( $attributes ) {
foreach ( $attributes as $attribute_key => $attribute ) {
if( in_array( $attribute_key, array( 'size', 'gender' ) ) ){
// Modify the attributes meta.
$attributes[ $attribute_key ]['is_variation'] = 1;
}
}
}
update_post_meta( $product->get_id(), '_product_attributes', $attributes );
}
wp_reset_postdata();
}
}
Potential update for WooCommerce 3.0 CRUD functions, but still untested:
function so_41978670_run_once() {
$args = array(
'limit' => -1,
'type' => 'variable',
);
$products = wc_get_products( $args );
if( ! empty ( $products ) ) {
foreach ( $products as $product ) {
$attributes = $product->get_attributes();
if ( $attributes ) {
foreach ( $attributes as $attribute_key => $attribute ) {
if( in_array( $attribute_key, array( 'size', 'gender' ) ) ){
// Modify the attribute settings.
$attribute->set_variation( true );
}
}
}
$product->set_attributes( $attributes );
$product->save();
}
}
}
add_action( 'admin_init', 'so_41978670_run_once' );