How to Get WooCommerce Product Name with WP_Query? - mysql

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,
]);

Related

WP query retrieve the src of the attached image

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

yii 1 relation not working in CGridView

I am trying to get relation where companies table have primary key companyID and division table have Foreign key companyID , what I need in where clause is WHERE companies.companyID = division.companies
relation in my model is :
public function relations()
{
return array(
'company' => array(self::BELONGS_TO, 'Companies', 'CompanyID'),
);
}
My Model->search() function is
public function search()
{
$criteria=new CDbCriteria;
$criteria->with ='company';
$criteria->compare('company.CompanyID', $this->CompanyID, true );
$criteria->compare('DivisionID',$this->DivisionID, true);
$criteria->compare('CompanyID',$this->CompanyID, true);
$criteria->compare('Name',$this->Name,true, true);
return new CActiveDataProvider($this, array(
'criteria'=>$criteria,
));
}
and my admin.php view is:
<?php
$this->breadcrumbs = array(
'Divisions' => array('index'),
'Manage',
);
$this->menu = array(
array('label' => 'List Divisions', 'url' => array('index')),
array('label' => 'Create Divisions', 'url' => array('create')),
);
");
?>
<div class="row">
<?php
$this->renderPartial('_dropdownfilter', array(
'model' => $model,
));
?>
</div><!-- end dropdown partial form -->
<?php
$this->widget('zii.widgets.grid.CGridView', array(
'id' => 'divisions-grid',
'dataProvider' => $model->search(),
'filter' => $model,
'columns' => array(
'CompanyID',
'DivisionID',
'Name',
array(
'class' => 'CButtonColumn',
),
),
));
?>
You need to add together=true to your criteria.
$criteria->together = true;
It'll add join to query. Some information about lazy loading http://www.yiiframework.com/wiki/527/relational-query-lazy-loading-and-eager-loading-with-and-together/
If you want to display company name,just do this in view.Don't change anything in model->search().
array(
'name'=>'Name',
'value'=>$model->company->name //here name is column name in company table.
),
In your gridview code do the following changes.
$this->widget('zii.widgets.grid.CGridView', array(
'id' => 'divisions-grid',
'dataProvider' => $model->search(),
'filter' => $model,
'columns' => array(
array(
'name' => 'companies',//fied from division table which refers to companyId from company table.
'header' => 'Company',
'value' => '$data->company->company_name'
),
'CompanyID',
'DivisionID',
'Name',
array(
'class' => 'CButtonColumn',
),
),
));
And in your model->search()
public function search()
{
$criteria=new CDbCriteria;
$criteria->with ='company';
$criteria->compare('company.company_name', $this->companies, true );
$criteria->compare('DivisionID',$this->DivisionID, true);
$criteria->compare('CompanyID',$this->CompanyID, true);
$criteria->compare('Name',$this->Name,true, true);
return new CActiveDataProvider($this, array(
'criteria'=>$criteria,
));
}

WP_Query parameters not working?

So I'm trying to get pagination correct for my wordpress site so categories and pages display the correct posts :
wp_reset_query();
$paged = get_query_var( 'paged' ) ? get_query_var( 'paged' ) : 1;
$sticky = get_option( 'sticky_posts' );
$args = array(
'ignore_sticky_posts' => 1,
'posts_per_page' => 10,
'orderby' => 'date',
'post__not_in' => $sticky,
'paged' => $paged,
);
$query = new WP_Query( $args );
?>
<?php if (have_posts()) : ?>
<?php while (have_posts()) : the_post(); ?>
I get the posts to display but the following parameters :
'ignore_sticky_posts' => 1,
'posts_per_page' => 10,
'orderby' => 'date',
'post__not_in' => $sticky,
does not seem to work... any idea why ?
Chances are you don't need wp_reset_query() at the top unless you are overriding the global $wp_query somewhere else in your code.
When you are creating a custom query you need to use that throughout your loop. In your case that is $query so we need to use that when calling have_posts(), the_post(), and max_num_pages
$paged = get_query_var( 'paged' ) ? get_query_var( 'paged' ) : 1;
$sticky = get_option( 'sticky_posts' );
$args = array(
'ignore_sticky_posts' => 1,
'posts_per_page' => 10,
'orderby' => 'date',
'post__not_in' => $sticky,
'paged' => $paged,
);
$query = new WP_Query( $args );
?>
<?php if ( $query->have_posts() ) : ?>
<?php while ( $query->have_posts() ) : $query->the_post(); ?>
// interact with the post here
<?php endwhile; ?>
<?php endif; ?>
// we overwrote the global $post when called the_post(); so we need to reset that
<?php wp_reset_postdata(); ?>
// again we need to reference our custom query for our pagination
<?php
$big = 999999999; // need an unlikely integer
echo paginate_links( array(
'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
'format' => '?paged=%#%',
'current' => max( 1, get_query_var('paged') ),
'total' => $query->max_num_pages
) );
?>

Save an object in wordpress database

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()

Issue on Adding Taxonomy to Custom Post Type Using Function

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',
) );
}