post_where clause to exclude post attachment from result - sql

I'm having an issue with my custom 'where' clause as it would also include the post attachment on the search results.
Here's the filter function
function title_filter( $where, $wp_query ){
global $wpdb;
if( $search_term = $wp_query->get( 'title_filter' ) ) :
$search_term = $wpdb->esc_like( $search_term );
$search_term = ' \'%' . $search_term . '%\'';
$title_filter_relation = ( strtoupper( $wp_query->get( 'title_filter_relation' ) ) == 'OR' ? 'OR' : 'AND' );
$where .= ' '.$title_filter_relation.' ' . $wpdb->posts . '.post_title LIKE ' . $search_term;
endif;
return $where;
}
and here are my parameters
$args = array(
'post_type' => 'bb_destinations',
'posts_per_page' => -1,
'suppress_filters' => false,
'title_filter' => $search,
'title_filter_relation' => 'OR',
'meta_query' => $meta_query,
'tax_query' => $tax_query,
'meta_key' => 'offer_repeater_%_destination_offer_price',
'orderby' => 'meta_value',
'order' => 'ASC',
);
it seems the search term would also include the attachments uploaded to the post which is weird. How do I exclude them?
for example, I would search the term 'Zia Hotel', it would also include the attachments uploaded to that post.

Related

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.

Prestashop custom admin module draggable sort/order not working

I'm building a very simple module for Prestashop 1.6 and I added an admin interface that allows to list my records, a form to add and edit and delete.
This is working fine as you can see here:
The problem is that draggable reorder button is not draggable. Hence the reordering is not working...
According to the official docs, if you set the position option in your controller, you get the draggable functionality:
['position'] => 'position', // If set to position, the field will display arrows and be drag and droppable, which will update position in db (optional).
This is the relevant part of my module controller:
$this->fields_list = array(
'id_quicklinks' => array(
'title' => $this->l('ID'),
'align' => 'center',
'width' => 25
),
'titulo' => array(
'title' => $this->l('Titulo'),
'width' => 'auto'
)
, 'lead' => array(
'title' => $this->l('Subtitulo'),
'width' => 'auto'
),
'position' => array(
'title' => $this->l('Ordem'),
'filter_key' => 'a!position',
'position' => 'position',
'align' => 'center',
'class' => 'fixed-width-md'
),
'active' => array(
'title' => $this->l('Publicado'),
'width' => '25',
'active' => 'status'
)
);
As you can see in the print screen, is shows the handles but the drag-an-drop doesn't work. No javascript errors on the console, no nothing... And I can see in the source code that jQuery and jQueryUI are loaded. Other admin pages with reorder feature are working fine...
Any ideas?
Thanks.
Well, if anyone is interested, I managed to get this to work.
In my module admin controller, I've added (before the __construct method, right after opening the Class):
protected $position_identifier = 'id_quicklinks';
id_quicklinks is the primary key of the database table this module uses.
This enabled the drag and drop feature that I was looking for, but altough I could now drag and drop, the order was not saved in the database.
For that to work I've added two more methods adapted from the controllers/admin/AdminCarriersController.php and classes/Carrier.php:
public function ajaxProcessUpdatePositions()
{
$way = (int)Tools::getValue('way');
$id_quicklinks = (int)Tools::getValue('id');
$positions = Tools::getValue('quicklinks');
if (is_array($positions))
foreach ($positions as $position => $value)
{
$pos = explode('_', $value);
if (isset($pos[2]) && (int)$pos[2] === $id_velcroquicklinks)
{
if (isset($position) && $this->updatePosition($way, $position, $id_quicklinks))
echo 'ok position '.(int)$position.' for id '.(int)$pos[1].'\r\n';
else
echo '{"hasError" : true, "errors" : "Can not update id '.(int)$id_quicklinks.' to position '.(int)$position.' "}';
break;
}
}
}
And:
public function updatePosition($way, $position, $id)
{
if (!$res = Db::getInstance()->executeS('
SELECT `id_quicklinks`, `position`
FROM `'._DB_PREFIX_.'quicklinks`
ORDER BY `position` ASC'
))
return false;
foreach ($res as $quicklinks)
if ((int)$quicklinks['id_quicklinks'] == (int)$id)
$moved_quicklinks = $quicklinks;
if (!isset($moved_quicklinks) || !isset($position))
return false;
var_dump($moved_quicklinks['position']);
// < and > statements rather than BETWEEN operator
// since BETWEEN is treated differently according to databases
return (Db::getInstance()->execute('
UPDATE `'._DB_PREFIX_.'quicklinks`
SET `position`= `position` '.($way ? '- 1' : '+ 1').'
WHERE `position`
'.($way
? '> '.(int)$moved_quicklinks['position'].' AND `position` <= '.(int)$position
: '< '.(int)$moved_quicklinks['position'].' AND `position` >= '.(int)$position.'
'))
&& Db::getInstance()->execute('
UPDATE `'._DB_PREFIX_.'quicklinks`
SET `position` = '.(int)$position.'
WHERE `id_quicklinks` = '.(int)$moved_quicklinks['id_quicklinks']));
}
I hope this helps someone with the same problem.
Thank you for your solution.
It took me quite a long time to realize that ajaxProcessUpdatePositions() is somehow call by itself. For those struggling to implement that method, note that echo and var_dump() won't print anything when the method is called when moving element in your table.
Also, I found someway to optimize your code a little bit.
You could replace:
if (!$res = Db::getInstance()->executeS('
SELECT `id_quicklinks`, `position`
FROM `'._DB_PREFIX_.'quicklinks`
ORDER BY `position` ASC'
))
return false;
foreach ($res as $quicklinks)
if ((int)$quicklinks['id_quicklinks'] == (int)$id)
$moved_quicklinks = $quicklinks;
By :
if (!$moved_quicklinks = Db::getInstance()->executeS('
SELECT `id_quicklinks`, `position`
FROM `'._DB_PREFIX_.'quicklinks`
WHERE `id_quicklinks` = ' . (int) $id . '
LIMIT 1;'
))
return false;
The fewer rows (and fields) returned by the database is the best. Moreover, it remove the need of a loop in your PHP code.
You could also merge both your update queries in the same database call:
return (Db::getInstance()->execute('
UPDATE `'._DB_PREFIX_.'quicklinks`
SET `position`= `position` '.($way ? '- 1' : '+ 1').'
WHERE `position`
'.($way
? '> '.(int)$moved_quicklinks[0]['position'].' AND `position` <= '.(int)$position
: '< '.(int)$moved_quicklinks[0]['position'].' AND `position` >= '.(int)$position.'
') . '; UPDATE `'._DB_PREFIX_.'quicklinks`
SET `position` = '.(int)$position.'
WHERE `id_quicklinks` = '.(int)$moved_quicklinks[0]['id_quicklinks']);
Database calls are costly operations, so reducing them to the minimum is a must. Like in your PHP code, the second query won't be executed if the first fails.
Here's how my ajaxProcessUpdatePositions() looks like:
public function ajaxProcessUpdatePositions()
{
$id = (int) Tools::getValue('id');
$positions = Tools::getValue("priority_level");
$way = (boolean) Tools::getValue('way');
//"and $id" because an ID of 0 is invalid (at least if you are using an auto-increment PK)
if (is_array($positions) and $id)
{
foreach ($positions as $position => $value)
{
$pos = explode('_', $value);
if (isset($pos[2]) && (int) $pos[2] === $id)
{
$position = (int) $position;
if ($priorityLevel = Db::getInstance()->executeS(
'SELECT `id_priority_level`,
`priority_level`
FROM `pc_job_priority`
WHERE `id_priority_level` = ' . $id . '
LIMIT 1'
)) {
Db::getInstance()->execute(
'UPDATE `pc_job_priority`
SET `priority_level` = `priority_level` '.
($way ? '- 1' : '+ 1') . '
WHERE `priority_level`' . (
$way ?
' > '. (int) $priorityLevel[0]['priority_level'] .
' AND `priority_level` <= '. $position :
' < '. (int) $priorityLevel[0]['priority_level'] .
' AND `priority_level` >= '. $position
) . ';
UPDATE `pc_job_priority`
SET `priority_level` = ' . $position . '
WHERE `id_priority_level` = ' . $id
);
}
break;
}
}
}
}

ajaxlink shows blank # Yii

my ajaxlink shows # instead of the link. I tried using chtml::link, and that displayed it correctly.
<?php
echo CHtml::ajaxLink("<img alt=".$data->text."
src=".Yii::app()->assetManager->publish(Yii::app()->basePath."/images/".$data->img)."
title=".$data->text." height=100px width=100px/>",
Yii::app()->createUrl('controller/action',array('id'=>$data->id)),
array(
"type" => "post",
"data" => "js:{ids:$.fn.yiiGridView.getSelection('chck-id')}",
"update" => "#grid"));
?>
Look at the ajaxLink function
public static function ajaxLink($text,$url,$ajaxOptions=array(),$htmlOptions=array())
{
if(!isset($htmlOptions['href']))
$htmlOptions['href']='#';
$ajaxOptions['url']=$url;
$htmlOptions['ajax']=$ajaxOptions;
self::clientChange('click',$htmlOptions);
return self::tag('a',$htmlOptions,$text);
}
As you see href comes from htmlOptions. The right code will be:
CHtml::ajaxLink("<img alt='".$data->text."'
src='".Yii::app()->assetManager->publish(Yii::app()->basePath."/images/".$data->img)."'
title='".$data->text."' height=100px width=100px/>",
'',
array(
"type" => "post",
"data" => "js:{ids:$.fn.yiiGridView.getSelection('chck-id')}",
"update" => "#grid"
),array(
'href'=>Yii::app()->createUrl('controller/action',array('id'=>$data->id))
));
Hope your other approach worked! But you could try repeating the link for both url and href like this.
echo CHtml::ajaxLink("<img alt='" . $data->text . "'
src='" . Yii::app()->assetManager->publish(Yii::app()->basePath . "/images/" . $data->img) . "'
title='" . $data->text . "' height=100px width=100px/>",
Yii::app()->createUrl('controller/action', array('id' => $data->id)),
array(
"type" => "post",
"data" => "js:{ids:$.fn.yiiGridView.getSelection('chck-id')}",
"update" => "#grid"
), array(
'href' => Yii::app()->createUrl('controller/action', array('id' => $data->id))
)
);

WP_Query() and ordering items

I have to order items from wp_post by their menu_order (pages only).
I've written this line:
$query = new WP_Query();
$all = $query -> query(array('post_type' => 'page', 'posts_per_page' => -1, 'orderby' => 'menu_order', 'order' => 'DESC'));
$children = get_page_children($id, $all);
foreach ($children as $child) {
if ($child -> ID == get_the_id()) {
echo '<li class="active">' . $id . $child -> post_title . '</li>';
I see the items but they are not ordered.
Thanks.
FZ
I would say that the issue relates to the fact you are passing the $all object through get_page_children before using it in a loop.
Why don't you just forget get_page_children and add 'post_parent' to your list of WP_Query arguments?
$all = $query->query(array('post_type' => 'page', 'post_parent' => $id, 'posts_per_page' => -1, 'orderby' => 'menu_order', 'order' => 'DESC'))
A helpful thread on Wordpress Stack Exchange;
https://wordpress.stackexchange.com/questions/35123/wp-get-all-the-sub-pages-of-the-parent-using-wp-query
EDIT.
To expand on comments. Sometimes there's confusion about menu_order - it does not relate to wp_nav_menus but to the Page Attributes Order input box, as shown below;