I'm doing a custom search in wordpress, and I would like to find my custom type articles by searching them from custom meta keys.
This code filters posts by custom types, but filters all of them by custom metas, showing no results!
function filtri_di_ricerca( $query ) {
if ( is_admin() || ! $query->is_main_query() )
return;
if (is_home())
{
$query->set('post_type', 'post_annunci');
$query->set('posts_per_page', 1000);
$meta_query = array();
// Only check these form fields
$fields = array( 'annuncio_contratto', 'annuncio_categorie', 'annuncio_tipologia', 'annuncio_citta', 'annuncio_prezzo');
foreach( $fields as $field ) {
if( $_GET[$field] != '' ) {
// We have something to match, otherwise ignore the field...
$meta_query[] = array(
'key' => $field,
'value' => $_GET[$field],
'compare' => '='
);
}
}
$meta_query['relation'] = 'AND';
$query->set('meta_query',$meta_query);
}
return;
}
add_action( 'pre_get_posts', 'filtri_di_ricerca');
The search address is:
http://example.com/?annuncio_contratto=Vendita&annuncio_categorie=Residenziale&annuncio_tipologia=Appartamento&annuncio_citta=NewYork
DEBUG with:
echo "<pre>"; print_r($wp_query->query_vars); echo "</pre>";
result:
Array
(
[error] =>
[m] => 0
[p] => 0
[post_parent] =>
[subpost] =>
[subpost_id] =>
[attachment] =>
[attachment_id] => 0
[name] =>
[static] =>
[pagename] =>
[page_id] => 0
[second] =>
[minute] =>
[hour] =>
[day] => 0
[monthnum] => 0
[year] => 0
[w] => 0
[category_name] =>
[tag] =>
[cat] =>
[tag_id] =>
[author_name] =>
[feed] =>
[tb] =>
[paged] => 0
[comments_popup] =>
[meta_key] =>
[meta_value] =>
[preview] =>
[s] =>
[sentence] =>
[fields] =>
[menu_order] =>
[category__in] => Array
(
)
[category__not_in] => Array
(
)
[category__and] => Array
(
)
[post__in] => Array
(
)
[post__not_in] => Array
(
)
[tag__in] => Array
(
)
[tag__not_in] => Array
(
)
[tag__and] => Array
(
)
[tag_slug__in] => Array
(
)
[tag_slug__and] => Array
(
)
[post_type] => post_annunci
[posts_per_page] => 1000
[meta_query] => Array
(
[0] => Array
(
[key] => annuncio_contratto
[value] => Vendita
[compare] => =
)
[1] => Array
(
[key] => annuncio_categorie
[value] => Residenziale
[compare] => =
)
[2] => Array
(
[key] => annuncio_tipologia
[value] => Appartamento
[compare] => =
)
[3] => Array
(
[key] => annuncio_citta
[value] => Pesaro
[compare] => =
)
[relation] => AND
)
[ignore_sticky_posts] =>
[suppress_filters] =>
[cache_results] => 1
[update_post_term_cache] => 1
[update_post_meta_cache] => 1
[nopaging] =>
[comments_per_page] => 50
[no_found_rows] =>
[order] => DESC
)
function filtri_di_ricerca( $query ) {
if ( is_admin() || ! $query->is_main_query() )
return;
if (is_home())
{
$query->set('post_type', 'post_annunci'); // added
$query->set('posts_per_page', 1000); //added
$meta_query = array(); //edited
// Only check these form fields
$fields = array( 'annuncio_contratto', 'annuncio_categorie', 'annuncio_tipologia', 'annuncio_citta', 'annuncio_prezzo');
foreach( $fields as $field ) {
if( isset($_GET[$field]) and $_GET[$field] != '' ) { // ADDED isset()
// We have something to match, otherwise ignore the field...
$meta_query[] = array(
'key' => $field,
'value' => $_GET[$field],
'compare' => '=', // CHANGED
);
}
}
$meta_query['relation'] = 'AND'; // ADDED
$query->set('meta_query',$meta_query);
}
return;
}
add_action( 'pre_get_posts', 'filtri_di_ricerca');
EDIT
You went wrong when you put main query data and meta query data in the same basket.
Related
How can i display custom post type posts list in elementor select control
This is my code =>
I have used this but this shows post categories not post title
$options = array();
$posts = get_posts( array(
'post_type' => 'digital_card'
) );
foreach ( $posts as $key => $post ) {
setup_postdata( $post );
$options[$post->ID] = get_the_title();
}
$this->add_control(
'show_elements',
[
'label' => __( 'Select Posts', 'plugin-domain' ),
'label_block' => ('bool'),
'type' => \Elementor\Controls_Manager::SELECT,
'multiple' => true,
'options' => $options,
]
);
Try the below code.
$options = array();
$posts = get_posts( array(
'post_type' => 'digital_card'
) );
foreach ( $posts as $key => $post ) {
$options[$post->ID] = get_the_title($post->ID);
}
public static function getLocations()
{
$posts = get_posts(array('post_type' => 'location'));
$allPosts = array();
foreach ($posts as $key => $post) {
array_push(
$allPosts,
array(
'map_latitude' => $post->post_author,
'pin_title' => $post->post_title,
)
);
}
return $allPosts;
}
$this->add_control(
'id',
array(
'label' => __('Location Map Pins', 'textdomain'),
'type' => Controls_Manager::REPEATER,
'default' => Hip_Maps_Elementor_Widget::getLocations(),
'fields' => $repeater->get_controls(),
'title_field' => '{{{ pin_title }}}',
'prevent_empty' => false,
)
);
I am authorizing payment through a wordpress website[woocommerce - 2.4.12]. After that I am using "DoCapture Api" for Capture Authorized payment to automate the system.
https://developer.paypal.com/docs/classic/api/merchant/DoCapture_API_Operation_NVP/
I am using below code in wordpress/woocommerce[2.4.12]
function get_capture_request( $order, $amount = null ) {
$request = array(
'VERSION' => '84.0',
'SIGNATURE' => 'SIGNATURE',
'USER' => 'USER',
'PWD' => 'PWD',
'METHOD' => 'DoCapture',
'AUTHORIZATIONID' => get_post_meta($order->id, '_transaction_id', true ),
'AMT' => number_format( is_null( $amount ) ? $order->get_total() : $amount, 2, '.', '' ),
'CURRENCYCODE' => 'CAD',
'COMPLETETYPE' => 'Complete',
);
return apply_filters( 'woocommerce_paypal_capture_request', $request, $order, $amount );
}
$sandbox = 'https://api-3t.sandbox.paypal.com/nvp';
$raw_response = wp_safe_remote_post($sandbox,
array(
'method' => 'POST',
'body' => get_capture_request( $order, $amount ),
'timeout' => 70,
'user-agent' => 'WooCommerce/' . WC()->version,
'httpversion' => '1.1',
)
);
In response I am getting below response.
stdClass Object
(
[AUTHORIZATIONID] => AUTHORIZATIONID
[TIMESTAMP] => 2019-09-15T08:52:50Z
[CORRELATIONID] => CORRELATIONID
[ACK] => Success
[VERSION] => 84.0
[BUILD] => 53497452
[TRANSACTIONID] => TRANSACTIONID
[PARENTTRANSACTIONID] => PARENTTRANSACTIONID
[PAYMENTTYPE] => instant
[ORDERTIME] => 2019-09-15T08:52:48Z
[AMT] => 6.24
[FEEAMT] => 0.48
[CURRENCYCODE] => CAD
[PAYMENTSTATUS] => Pending
[PENDINGREASON] => paymentreview
[REASONCODE] => None
[SHIPPINGMETHOD] => Default
[PROTECTIONELIGIBILITY] => Ineligible
[PROTECTIONELIGIBILITYTYPE] => None
)
but I don't know why I am getting PAYMENTSTATUS Pending and also in pending reason there is no "paymentreview" option[ https://developer.paypal.com/docs/classic/api/merchant/DoCapture-API-Operation-NVP/ ].
Can any one guide me what is the problem here?
Thank in advance.
I have filter in my website:
My filter form
Filtering algorithm is:
Filtering algorithm
PHP code for this algorithm:
$GLOBALS['my_query_filters'] = array(
'field_1' => 'work',
'field_2' => 'visa'
);
add_action('pre_get_posts', 'my_pre_get_posts', 10, 1);
function my_pre_get_posts( $query ) {
if(is_admin()) return;
if(!$query->is_main_query()) return;
$meta_query = $query->get('meta_query');
$tax_query = $query->get('tax_query');
$k = 0;
foreach($GLOBALS['my_query_filters'] as $key => $name) {
if(empty($_GET[$name])) continue;
$value = explode(',', $_GET[$name]);
foreach($value as $item) {
$meta_query[$k] = array(
'key' => $name,
'value' => $item,
'type' => 'CHAR',
'compare' => 'LIKE'
);
$k++;
}
}
if($_GET['cat']) {
$tax_query[] = array(
array(
'taxonomy' => 'category',
'field' => 'id',
'terms' => $_GET['cat'],
'operator' => 'IN'
)
);
$query->set('tax_query', $tax_query);
}
$query->set('meta_query', $meta_query);
}
But I need change filtering algorithm to:
New filtering algorithm
I do not know much WP_Query(). Can you hellp me edit $meta_query?
I changed filter to:
function my_pre_get_posts( $query ) {
if(is_admin()) return;
if(!$query->is_main_query()) return;
$meta_query = $query->get('meta_query');
$tax_query = $query->get('tax_query');
foreach($GLOBALS['my_query_filters'] as $key => $name) {
if(empty($_GET[$name])) continue;
$meta_query['relation'] = 'AND';
$meta_query_inner['relation'] = 'OR';
$value = explode(',', $_GET[$name]);
$k = 0;
foreach($value as $item) {
$meta_query_inner[$k] = array(
'key' => $name,
'value' => $item,
'type' => 'CHAR',
'compare' => 'LIKE'
);
$k++;
}
$meta_query[] = $meta_query_inner;
}
if($_GET['cat']) {
$tax_query[] = array(
array(
'taxonomy' => 'category',
'field' => 'id',
'terms' => $_GET['cat'],
'operator' => 'IN'
)
);
$query->set('tax_query', $tax_query);
}
$query->set('meta_query', $meta_query);
}
It seems to work :)
I have a DataObject Confirmation that looks like this:
<?php
class Confirmation extends DataObject
{
private static $db = array(
'Reimbursement' => 'Money',
'SomeText' => 'Varchar(255)'
);
private static $has_one = array(
'Page' => 'Page'
);
public function getCMSFields()
{
$fields = parent::getCMSFields();
$reimbursementField = MoneyField::create('Reimbursement');
$someTextField = TextField::create('SomeText');
$reimbursementField->setAllowedCurrencies(array('SEK'));
$fields->addFieldsToTab(
'Root.Main',
array(
$reimbursementField,
$someTextField
)
);
return $fields;
}
public function onBeforeWrite()
{
parent::onBeforeWrite();
if($this->isChanged('Reimbursement')) SS_Log::log( print_r ( 'changed', true ), SS_Log::WARN );
}
}
if($this->isChanged('Reimbursement')) in the onBeforeWrite() function will always evaluate to true. Regardless of what is being changed. Even when saving without making any changes - it will fire the log function.
Edit: Checking the stacktrace with SS_Backtrace::backtrace() in Money.php's setValue() function I find that every time I save the Confirmation DataObject, setValue() is called without passing the $markChanged parameter set to false, from DataObject.php line 1281, resulting in the $isChanged flag being set to true, and the field always behaving as if it has changed.
When logging DataObject's getChangedFields() function I get the output below (when saving without making any changes). Notice the Reimbursement field being present, and before being empty. What am I missing here?
[22-Mar-2016 16:28:07] Warning at framework/model/DataObject.php line 2597: Array
(
[Reimbursement] => Array
(
[before] =>
[after] => Money Object
(
[currency:protected] => SEK
[amount:protected] => 25000
[isChanged:protected] => 1
[locale:protected] =>
[currencyLib:protected] => Zend_Currency Object
(
[_options:protected] => Array
(
[position] => 8
[script] =>
[format] =>
[display] => 2
[precision] => 2
[name] => svensk krona
[currency] => SEK
[symbol] => kr
[locale] => sv_SE
[value] => 0
[service] =>
[tag] => Zend_Locale
)
)
[allowedCurrencies:protected] =>
[value:protected] =>
[tableName:protected] =>
[name:protected] => Reimbursement
[arrayValue:protected] =>
[defaultVal:protected] =>
[failover:protected] =>
[customisedObject:protected] =>
[objCache:ViewableData:private] => Array
(
)
[class] => Money
[extension_instances:protected] => Array
(
)
[beforeExtendCallbacks:protected] => Array
(
)
[afterExtendCallbacks:protected] => Array
(
)
)
[level] => 2
)
)
I am having a bit of trouble with an import/export option I'm building into my theme. Hopefully I explain this well, let me start with the function.
function ifeature_import_options() {
$options = get_option('ifeature');
$import = $options['if_import_code'];
if ($import != '');
$options_array = (unserialize($import));
foreach( $options_array as $option_name => $option_value ) {
update_option( $option_name, $option_value );
}
}
if_import_code returns the serialized output of $options (which the user enters into a textarea in my options panel), and I've tested that $options_array is properly unserializing the import code as it prints the following:
Array ( [if_menu_color] => Pink [if_font] => Arial [if_css_options] =>
[if_favicon] => [if_ga_code] => [if_import_code] => [if_export_code]
=> b:0; [if_logo] => [if_menuicon] => [if_header_contact] =>
[if_facebook] => [if_twitter] => [if_linkedin] => [if_youtube] =>
[if_googlemaps] => [if_email] => [if_rsslink] => [if_home_description]
=> [if_home_keywords] => [if_home_title] => [if_callout_title] =>
[if_callout_text] => [if_callout_img] => [if_callout_image_link] =>
[if_slider_type] => posts [if_slider_placement] => feature
[if_slider_category] => [if_slider_posts_number] => [if_slider_height]
=> [if_slider_delay] => [if_slider_animation] => random
[if_footer_text] => [if_hide_callout] => 0 [if_show_fb_like] => 0
[if_hide_slider] => 0 [if_hide_boxes] => 0 [if_hide_link] => 0
[if_slider_navigation] => 0 )
So I have the new array that needs to be written to my options, but the update_options code I've used isn't working. What am I missing?
$options_array = unserialize($importOptions);
foreach ($options_array as $option_name=>$value) {
if ($value != '') update_option( $key, $value );
}