Get images by custom field - sql

I'm trying to display all images that have a certain custom field from the types plugin set to true. It would also work to filter them by post_content or post_excerpt but none of my attempts have worked so far.
<?
$args = array(
'post_type' => 'attachment',
'post_mime_type' => 'image',
'post_content' => 'foo',
'numberposts' => -1
);
?>
<? print_r(get_posts($args)); ?>
This get's all images allthough only one has the post_content foo. My attempt to use WP_Query failed miserably as well.
Any help is appreciated!

WP_Query method :
$args = array(
'post_type' => 'attachment',
'post_status' => 'inherit',
'meta_query' => array(
array(
'key' => 'color',
'value' => 'blue',
'compare' => 'LIKE',
),
),
);
$query = new WP_Query( $args );
I am presuming that the reason why you failed with WP_Query is due to the following condition.
Codex states : The default WP_Query sets 'post_status'=>'publish', but attachments default to 'post_status'=>'inherit' so you'll need to explicitly set post_status to 'inherit' or 'any' as well.
http://codex.wordpress.org/Class_Reference/WP_Query#Custom_Field_Parameters
get_posts method :
$args = array(
'post_type' => 'attachment',
'post_mime_type' => 'image',
'meta_key' => 'custom-field',
'meta_value' => 'custom value',
'numberposts' => -1
);
print_r(get_posts($args));
The only draw back with this method is that the meta_value needs to exactly match what was entered in the custom field. If you still like to use get_posts then use the meta_query as shown in WP_Query example above.

Related

Existence of posts from complex WP_Query

I have a comparative set of arguments for WP_Query involving a custom field.
On a page I need to say "Are there going to be results?, if so display a link to another page that displays these results, if not ignore" There are between 500 and 1200 posts of this type but could be more in the future. Is there a more efficient or direct way of returning a yes/no to this query?
$args = array(
'post_type' => 'product',
'posts_per_page' => -1,
'meta_query' => array(
array(
'key' => 'partner',
'value' => $partner,
'compare' => 'LIKE',
),
),
);
$partner_query = new WP_Query($args);
if ($partner_query->have_posts() ) { [MAKE LINK] }
The link is not made from data returned, we already have that information.
Perhaps directly in the database. My SQL is not up to phrasing the query which in English is SELECT * from wp_posts WHERE post_type = 'product'} AND (JOIN??) post_meta meta_key =
partner AND post_id = a post_id that matches the first part of the query.
And if I did this, would this be more efficient that the WP_Query method?
Use 'posts_per_page' => 1 and add 'no_found_rows' => true and 'fields' => 'ids'. This will return the ID of a matching post, and at the same time avoid the overhead of counting all the matching posts and fetching the entire post contents. Getting just one matching post id is far less work than counting the matching posts. And it's all you need.
Like this:
$args = array(
'post_type' => 'product',
'posts_per_page' => 1,
'no_found_rows' => true,
'fields' => 'ids',
'meta_query' => array(
array(
'key' => 'partner',
'value' => $partner,
'compare' => 'LIKE',
),
),
);
$partner_query = new WP_Query($args);
if ($partner_query->have_posts() ) { [MAKE LINK] }
no_found_rows means "don't count the found rows", not "don't return any found rows". It's only in the code, not the documentation. Sigh.

shopware 6 upload multiple product variation images

Can we upload multiple image to the variation using API. Single image upload can be possible using below code, but not sure about multiple image upload.
$mediaId ='random string';
$url = $images['url'];
$mediaData = array(
array(
'id' => $mediaId,
'mediaFolderId' => $mediaFolderID,
),
);
$mediaDataArr = array(
'payload' => array(
"action" => "upsert",
"entity" => "media",
'payload' => $mediaData,
),
);
$createMedia = $shopware6HelperObj->post('_action/sync', $mediaDataArr);
$urlArr = array(
'url' => $url,
);
$parts = pathinfo($url);
$params = array(
'extension' => $parts['extension'],
'fileName' => $parts['filename'] . '__' . md5(time()),
);
$uploadImage = $shopware6HelperObj->post('_action/media/' . $mediaId . '/upload', $urlArr, $params);
Please suggest if any idea.
I always recommend to try doing that via the Admin Panel and check the Browser's Dev Tools / Network Tab which requests are made. This can give you a good hint on how do to it programmatically.

Update Post status when post is expire

I want to update post status when a post is expiring.
I have saved expiry date in the WordPress post meta (post_price_plan_expiration_date).
I know how to get an expired post with wp_query,
But I want to use SQL query to update post status.
$todayDate = strtotime(date('m/d/Y h:i:s'));
$args = array(
'post_type' => 'post',
'posts_per_page' => -1,
'meta_query' => array(
array(
'key' => 'featured_post',
'value' => '1',
'compare' => '=='
),
array(
'key' => 'post_price_plan_expiration_date',
'value' => $todayDate,
'compare' => '<='
),
)
);
$wp_query = new WP_Query($args);
print_r($wp_query);
This code returns me correct posts which I need, But I need to write the same query in SQL, And run that with wp_schedule_event
Any help???
You can always do the following out of a WP_Query
$wp_query = new WP_Query( $args );
echo $results->request;
Which should display the generated SQL Query.
Hope this helps.

WP_Query $args and compare operators

I'm having troubles to generate an sql query with operators.Actually wirte this code:
$args = array(
'post_type' => 'listings',
'posts_per_page' => 10,
'paged' => $paged,
'meta_query' => array(
//CITY
array(
'key' => 'city',
'value' => $city,
'compare' => 'LIKE'
),
//type
array(
'key' => 'type',
'value' => $type,
'compare' => 'LIKE'
),
//PRICE
array(
'key' => 'price',
'value' => array( 100, 5000 ),
'compare' => 'BETWEEN'
),
)
);
This code work fine, but now, i need to get the posts with price between $start and $end, OR price = -1. In other words, post that not have set the price, or set to -1 must show in the query. The others conditions (city, type,etc) must be match with AND
Any ideas? Thanks!
My initial thought is to add a filter onto posts_where or posts_join to inject a custom SQL condition into the query.
Something along these lines:
wp_postmeta.meta_key='price' AND
(wp_postmeta.meta_value='-1' OR
CAST(wp_postmeta.meta_value AS INT) BETWEEN $start AND $end)

BootGridView Filter not working in Yii Framework

I have tried to implement the sorting using BootGridView but it does not seem to work. Here is the line of code.
<?php $this->widget('bootstrap.widgets.BootGridView', array(
'id'=>'user-grid',
'dataProvider'=>$model->search(),
'filter'=>$model,
'columns'=>array(
'user_id',
array('name'=>'user_type',
'value'=>'User::getType($data->user_type)'),
'username',
array('header'=>'Name',
'type'=>'raw',
'value'=> 'CHtml::link($data->firstname." ".$data->lastname, array("view", "id"=>$data->id))'
),
array('name' => 'status',
'type' => 'raw',
'value' => '$data->status == 1 ? "Active" : CHtml::link("In Active", "javascript:void(0);", array("id" => "active_" . $data->id, "onClick"=>"js:activate($data->id)"))'
),
/*
'email',
'dob',
'profession',
'hobby',
'height',
'weight',
'weight_taken_on',
'login_attempt',
'registration_date',
*/
array(
'class'=>'bootstrap.widgets.BootButtonColumn',
),
),
)); ?>
The code does not seem to sort the table as expected. What could be the possible problem in this. ?
Sorting is configured in the data provider, not in the grid view. So you should look into the search() method of your model. You need something like this:
return new CActiveDataProvider('User',array(
// ...
'sort' => array(
'attributes' => array(
'name',
'email',
// ...
Have a look at CSort and especially the attributes property there to learn more about sort options you can use here.