it is possibe to save an object in prefix_options in wordpress database like this one:
$arr_params = array( 'cat' => $display_category, 'product' => $single_post_ID );
Thanks
edit:
after make some changes, the code can't add an new array in exsisting array in the database:
$item= array(
'name' => $name ,
'prename' => $prename
);
print_r($item);
$options = get_option( 'options' );
if (empty($options['items'])) {
$options['items']=array();
add_option( 'options', $options );
$options = get_option( 'options' );
$options['items'] = array_push($options['items'], "$item");
update_option( 'options', $options );
}
else{
$options = get_option( 'options' );
$options['items'] = array_push($options['items'], "$item");
update_option( 'options', $options );
}
yes you can,
$arr_params = array( 'cat' => $display_category, 'product' => $single_post_ID );
if( get_option("_arr_params") === false ) {
add_option("_arr_params", $arr_params);
}
else {
// holds : array( 'cat' => $display_category, 'product' => $single_post_ID );
$my_param = get_option("_arr_params");
}
According to Edit Part: array_push() on works to add one or more elements not the array, you can use array_merge() in place of it, or second option is i already used in below codes.
$options['wphyper_orders'][] = $order_detail;
helpful link : get_option()
Related
I am trying to use this code on "Wp All Import". If there is a product name in the database, that product should be omitted, but the code will not work as is. What do I need to do for the code to work?
add_filter('wp_all_import_is_post_to_create', 'create_only_if_unique_custom_field', 10, 3);
function create_only_if_unique_custom_field( $continue_import, $data, $import_id ) {
// Only run for import ID 1.
if ( $import_id == 33 || $import_id == 34 ) {
// The custom field to check.
$key_to_look_up = "post_title";
// The value to check where 'num' is the element name.
$value_to_look_up = $data['name'];
// Prepare the WP_Query arguments
$args = array (
// Set the post type being imported.
'post_type' => array( 'post' ),
// Check our custom field for our value.
'meta_query' => array(array(
'key' => $key_to_look_up,
'value' => $value_to_look_up,
)),
);
// Run the query and do not create post if custom field value is duplicated.
$query = new WP_Query( $args );
return !($query->have_posts());
} else {
// Take no action if a different import ID is running.
return $continue_import;
}
}
You can do like this.
<?php
$params = array('posts_per_page' => 5);
$wc_query = new WP_Query($params);
?>
<?php if ($wc_query->have_posts()) : ?>
<?php while ($wc_query->have_posts()) :
$wc_query->the_post(); ?>
<?php the_title(); ?>
<?php endwhile; ?>
<?php wp_reset_postdata();?>
<?php else: ?>
<p>
<?php _e( 'No Products' ); ?>
</p>
<?php endif; ?>
There are some ways to show the different types of Woocommerce product names with WP_Query.
<?php
//Pulling WooCommerce Products instead of WordPress Posts, Use this param
$params = array(
'posts_per_page' => 5,
'post_type' => 'product'
);
//Displaying products of a given price range, use this param
$params = array(
'posts_per_page' => 100,
'post_type' => array('product', 'product_variation'),
'meta_query' => array(
'relation' => 'OR',
array(
'key' => '_price',
'value' => 5,
'compare' => '<=',
'type' => 'NUMERIC'
),
array(
'key' => '_sales_price',
'value' => 5,
'compare' => '<=',
'type' => 'NUMERIC'
)
)
);
//Displaying available products only, use this param
$params = array(
'posts_per_page' => 5,
'post_type' => array('product', 'product_variation'),
'meta_query' => array(
array(
'key' => '_price',
'value' => 5,
'compare' => '<',
'type' => 'NUMERIC'
),
array(
'key' => '_stock_status',
'value' => 'instock'
)
)
);
$wc_query = new WP_Query($params);
if ($wc_query->have_posts()) :
while ($wc_query->have_posts()) :
$wc_query->the_post();
the_title();
endwhile;
endif;
Also will help you the article https://www.gavick.com/blog/wp_query-woocommerce-products
Thank you
This variable controls the title.
// Xml file column name
$value = = $data['product_title'];
// Get wpdb product title
$posts = get_posts([
'post_type' => 'product',
'title' => $value,
]);
I have a custom field in post_meta table of wordpress database. It's called 'combined'. I want to seperate the values based on key and save them as multiple rows if there are multiple values for the key.
My meta_key:
Result after I run my code:
What I want:
My Code:
add_action( 'init', function() {
if ( 'migrate' !== filter_input( INPUT_GET, 'action' ) ) {
return;
}
$query = new WP_Query( [
'posts_per_page' => -1,
'post_type' => 'post',
'post_status' => 'any',
] );
if ( ! $query->have_posts() ) {
return;
}
while ( $query->have_posts() ) {
$query->the_post();
$data = get_post_meta( get_the_ID(), 'combined', true );
foreach($data as $singleData => $value ){
update_post_meta( get_the_ID(), $singleData, $value );
}
}
} );
I have found the way to get the values as I want them to be in the database. Changing the loop part in the above code with code did the trick.
$data = get_post_meta( get_the_ID(), 'combined', true );
foreach($data as $singleData => $value ){
$thisArray = $data[$singleData];
foreach ($thisArray as $key2 => $value){
add_post_meta( get_the_ID(), $singleData, $thisArray[$key2], $unique = false);
}
}
I'm encoding in JSON a bunch of data from a WP Query:
$args = array(
'posts_per_page' => 20,
'post_type' => 'post',
'category' => 6,
'meta_key' => 'custom_total_hits',
'tag' => 'indie-pop',
'orderby' => 'meta_value_num',
'order' => 'DESC',
'date_query' => array(
'after' => date('Y-m-d', strtotime('-40 days'))
)
);
$query = new WP_Query( $args );
$posts = $query->get_posts();
foreach( $posts as $post ) {
$output[] = array(
'id' => $post->ID,
'title' => $post->post_title,
'count' => $post->custom_total_hits,
'soundcloud_url' => $post->soundcloud_song,
'soundcloud_id' => $post->soundcloud_ids,
'link' => get_permalink($post),
);
}
echo json_encode($output);
I would like to display in my JSON a key corrisponding to the src of the medium size of the attached image. If I use 'images' => get_attached_media('image', $post->ID) it retrives an array of multiple data which I can not access since I don't know the ID of the attached image when I process the data of my JSON. How can I do to retrieve a first level key - value where the value is the src of the attached image?
get_post_thumbnail_id : Get post thumbnail ID
wp_get_attachment_url : Get attachment URL by attachment id
'images' => parse_url( wp_get_attachment_url( get_post_thumbnail_id( $post->ID ) ) );
Would you please try above code?
Try this solution:
$images = array();
$post_thumbnail_id = get_post_thumbnail_id( $post->ID );
foreach (get_intermediate_image_sizes() as $size) {
$images[$size] = wp_get_attachment_image_src($post_thumbnail_id, $size);
}
//end
'images' => $images // type_of_size => image_url
Using WordPress 3.7.1 and PHP 5.4.12, I am trying to add Custom Post Type and Taxonomy to my Theme, so far the custom post type method works but the Taxonomy is not adding to the Admin Dashboard.
Here is the code I have:
<?php
function add_post_type($name, $args = array()) {
add_action('init', function() use($name, $args) {
$upper = ucwords($name);
$name = strtolower(str_replace(' ','_',$name));
$args = array_merge(
array(
'public'=> true,
'label' => "All $upper" . 's',
'labels' => array('add_new_item' => "Add New $upper"),
'support' => array('title', 'editor', 'author', 'thumbnail', 'excerpt', 'comments')
),
$args
);
register_post_type('$name', $args);
});
}
function add_taxonomy($name, $post_type, $args = array()) {
$name = strtolower($name);
add_action('init', function() use($name, $post_type, $args) {
$args = array_merge(
array(
'label' => ucwords($name),
),
$args
);
register_taxonomy($name, $post_type, $args);
});
}
add_post_type('book', array(
'supports' => array('title', 'editor', 'author', 'thumbnail', 'excerpt', 'comments')
));
add_taxonomy('fun', 'book');
?>
Can you please let me know what part I am doing wrong?
The $name variable is not parsed, put it between double quotes:
register_post_type( "$name", $args );
edit
add_action( 'init', 'so19966809_init' );
function so19966809_init()
{
register_post_type( 'book', array(
'public'=> true,
'label' => 'All Books',
'labels' => array( 'add_new_item' => 'Add New Book' ),
'supports' => array( 'title', 'editor', 'author', 'thumbnail', 'excerpt', 'comments' ),
'taxonomies' => array( 'fun' )
) );
register_taxonomy( 'fun', 'book', array(
'label' => 'Fun',
) );
}
I can (finally!!!) set a query to return results for a checkbox(es) set within a collapsible fieldset in Drupal 7- however when I try to put it into a tableselect, I get no results. Would someone be able to check out this code and see if you can tell me why? I also have a screen shot of both results- but since I am new here it won't allow me to post it.
$form = array();
$secnum = 1;
$result = db_query('SELECT s.secser_id, s.ser_name FROM {secser} s WHERE s.sec_num = :secnum', array(':secnum' => $secnum));
$options = array();
foreach ($result as $record) {
$options[$record->secser_id] = $record->ser_name;
}
$form['secser']['1'] = array(
'#title' => t('Basic Sanitation'),
'#type' => 'fieldset',
'#collapsible' => TRUE,
);
$form['secser']['1']['secser'] = array(
'#title' => t('Choices'),
'#type' => 'checkboxes',
'#multiple' => TRUE,
'#options' => $options,
'#description' => t('choose!'),
);
$form['secser']['2'] = array(
'#title' => t('Community Systems'),
'#type' => 'fieldset',
'#collapsible' => TRUE,
);
$secnum = 2;
$result = db_query('SELECT s.secser_id, s.ser_name FROM {secser} s WHERE s.sec_num = :secnum', array(':secnum' => $secnum));
$opt2 = array();
foreach ($result as $record) {
$opt2[$record->secser_id] = $record->ser_name;
}
$header = array(
'ser_name' => t('Choose Service(s)'),
);
$form['secser']['2']['secser'] = array(
'#type' => 'tableselect',
'#title' => t('Community Systems'),
'#header' => $header,
'#options' => array($opt2),
'#multiple' => TRUE,
);
I haven't tried running your code, but it looks like your options are an array which contains another array.
#options' => array($opt2),
because $opt2 is an array, this should be:
#options' => $opt2,
the same way you have the options set up in your checkboxes