Extracting optionValue from postmeta - php

I need to find all addresses from a wp database so I select all rows having metakey = mkd_portfolios having opionLabel = Address from postmeta table.
But I can't find how to extract items in meta_value.
I guess it is something easy with php?
meta_value : a:1:{i:0;a:4:
{s:11:""optionLabel"";s:7:""Address"";s:11:""optionValue"";s:21:""Dune du Pilat,
France"";s:9:""optionUrl"";s:0:"""";s:22:""optionlabelordernumber"";s:1:""1"";}}
meta_id: 15361
post_id: 11428

There is the WP_Meta_Query class.
$meta_query_args = array(
array(
'key' => 'optionLabel',
'value' => 'Address',
'compare' => '='
)
);
$meta_query = new WP_Meta_Query( $meta_query_args );
You might want to further narrow down your results via the AND relation in the query, I can not tell from the code you posted. Find more info at the link provided.

Related

Wordpress sort by meta values

I have an issue I am trying to solve right now and having an issue finding a way to do it properly.
I have a college lecture series I need to sort I can sort it by a meta field for year just fine. But when I get to sorting by semester it sorts it in alphabetical and so it ends up sorting by Winter,Spring,Fall or Fall,Spring,Winter depending on if I tell it ASC or DESC.
I need to figure out how to do order this in the right order preferably without adding another field to the posts for sort priority
Query args currently written as follows which is obviously a bit messy.
$args = array(
'post_type' => 'mcm_geri-ed',
'posts_per_page' => $posts_per_page,
'meta_query' => array(
'semester' => array(
'key' => 'semester',
),
'year' => array(
'key' => 'year'
)
),
'orderby' => array(
'year' => 'DESC',
'semester' => array(
'value' => 'date'
)
),
'paged' => $paged
);
I don't think there is a way to do this with WP_Query directly. How about using posts_orderby and manipulating the ORDER-part of the SQL to be something like meta_year DESC, meta_semester = 'Spring' DESC, meta_semester = 'Fall' DESC. Look at the generated SQL to see what names you should use for meta_year and meta_semester (or trimester, really).
This isn't as effective as changing semester to a numerical value and adding the name while outputting, but it will work.
This is what I ultimately did. It was something different than what was suggested but using the filter suggested to get the result. The if statement has a couple of other caveats added to it due to the way I've built a sorting system for the page template. I also wrote this in a test environment hence the different post type from the original code.
// Custom Orderby filter to return the correct order for semester values.
add_filter('posts_orderby', 'orderby_pages_callback', 10, 2);
function orderby_pages_callback($orderby_statement, $wp_query) {
//Making sure this only happens when it needs to. Which is the right post type and if it isn't getting sorted by other functions.
if ($wp_query->get("post_type") === "test_semester" && !is_admin() && !isset($_GET['order']) && !isset($_GET['sort'])) {
return "CAST(mt1.meta_value AS CHAR) DESC, wp_postmeta.meta_value = 'Fall' DESC, wp_postmeta.meta_value = 'Winter' DESC, wp_postmeta.meta_value = 'Spring' DESC,wp_postmeta.meta_value = 'Summer' DESC";
} else {
//return your regularly scheduled programming
return $orderby_statement;
}
}

get_users() when meta_key has multiple serialized values

I can't seem to find any information online, but what I am trying to do is to get an array of all users based on a value in a serialised meta_key, for example:
$get_users = get_users(array(
'meta_key' => 'value_one_of_array'
));
Then in the db, the meta_value columns would have two stored values such as:
meta_key
value_one_of_array , value_two_of_array
Is this possible?
May be it's help you.
$args = array();
if($values_of_array){
foreach($values_of_array as $key=>$val){
$args['meta_query'][] = array(
'key' => '#your_meta_key',
'value' => $val
);
}
$get_users = get_users($args);
}

data escaping remove for specific filed in cakephp

I am using subquery for id field.
$db = $this->AccountRequest->getDataSource();
$subQuery = $db->buildStatement(
array(
'fields' => array('MAX(id)'),
'table' => $db->fullTableName($this->AccountRequest),
'alias' => 'MaxRecord',
'limit' => null,
'offset' => null,
'order' => null,
'group' => array("user_id")
),
$this->AccountRequest
);
$searching_parameters = array(
#"AccountRequest.id IN " => "(SELECT MAX( id ) FROM `account_requests` GROUP BY user_id)"
"AccountRequest.id IN " => "(".$subQuery.")"
);
$this->Paginator->settings = array(
#'fields' => array('AccountRequest.*'),
'conditions' => $searching_parameters,
'limit' => $limit,
'page' => $page_number,
#'group' => array("AccountRequest.user_id"),
'order' => array(
'AccountRequest.id' => 'DESC'
)
);
$data = $this->Paginator->paginate('AccountRequest');
This structure is producing a query is:
SELECT
`AccountRequest`.`id`,
`AccountRequest`.`user_id`,
`AccountRequest`.`email`,
`AccountRequest`.`emailchange`,
`AccountRequest`.`email_previously_changed`,
`AccountRequest`.`first_name`,
`AccountRequest`.`first_namechange`,
`AccountRequest`.`f_name_previously_changed`,
`AccountRequest`.`last_name`,
`AccountRequest`.`last_namechange`,
`AccountRequest`.`l_name_previously_changed`,
`AccountRequest`.`reason`,
`AccountRequest`.`status`,
`AccountRequest`.`created`,
`AccountRequest`.`modified`
FROM
`syonserv_meetauto`.`account_requests` AS `AccountRequest`
WHERE
`AccountRequest`.`id` IN '(SELECT MAX(id) FROM `syonserv_meetauto`.`account_requests` AS `MaxRecord` WHERE 1 = 1 GROUP BY user_id)'
ORDER BY
`AccountRequest`.`id` DESC
LIMIT 25
In the subquery, its add an extra single quote so it's producing an error.
So, How can I remove these single quotes from this subquery?
Thanks
What are you trying to achieve with the sub query?
The MAX(id) just means it will pull the id with the largest value AKA the most recent insert. The sub query is completely redundant when you can just ORDER BY id DESC.
using MAX() will return only one record, if this is what you want to achieve you can replicate by adding LIMIT 1
If the sub query is just an example and is meant to be from another table I would just run the query that gets the most recent id before running the main query. Getting the last inserted id in a separate query is very quick and I cant see much of a performance loss. I think it will result in cleaner code that`s easier to follow to.
edit 1: From the comments it sounds like all your trying to get is a particular users latest account_requests.
You dont need the sub query at all. My query below will get the most recent account record for the user id you choose.
$this->Paginator->settings = array(
'fields' => array('AccountRequest.*'),
'conditions' => array(
'AccountRequest.user_id' => $userID // you need to set the $userID
)
'page' => $page_number,
'order' => array(
'AccountRequest.id DESC' //shows most recent first
),
'limit' => 1 // set however many you want the maximum to be
);
The other thing you cold be meaning is to get multiple entries from multiple users and display them in order of user first and then the order of recent to old for that user. MYSQL lets you order by more than one field, in that case try:
$this->Paginator->settings = array(
'conditions' => array(
'AccountRequest.user_id' => $userID // you need to set the $userID
)
'page' => $page_number,
'order' => array(
'AccountRequest.user_id', //order by the users first
'AccountRequest.id DESC' //then order there requests by recent to old
)
);
If the example data you have added into the question is irrelevant and you are only concerned about how to do nested subqueries it has already been answered here
CakePHP nesting two select queries
However I still think based on the data in the question you can avoid using a nested query.

How to search two meta values from same single column and table

I make a custom search in WordPress. There are three fields for searching.
Age
Location
Post Title
Post Title is searched from wp_posts > post_title field and location and age are custom meta fields y_age, y_activity_locations. So there is 6 scenarios of searching.
If user enter:
Enter Title or
Select Location or
Select Age or
Enter Title and Select Location or
Enter Title and Select Age or
Select Location and Select Age
My first 5 (Five) scenarios are working perfectly but my 6th scenario is not working because it comes from same table (wp_postmeta) and same column but two different values.
So I tried this query:
Select y_posts.*, y_meta.*
From wp_posts As y_posts
Inner Join wp_postmeta As y_meta
On y_posts.ID = y_meta.post_id
Where y_posts.post_type = 'download'
And y_posts.post_status = 'publish'
And y_meta.meta_key = 'y_activity_locations'
And y_meta.meta_value Like '%Armidale%'
And y_meta.meta_key = 'y_age'
And y_meta.meta_value Like '%3 to 5%'
The query is not working because I think sever confused two values from same column.
Select y_posts.*, y_meta.*
From wp_posts As y_posts
Inner Join wp_postmeta As y_meta
On y_posts.ID = y_meta.post_id
Where y_posts.post_type = 'download'
And y_posts.post_status = 'publish'
And y_meta.meta_key = 'y_activity_locations'
And y_meta.meta_value Like '%Armidale%'
Or y_meta.meta_key = 'y_age'
Or y_meta.meta_value Like '%3 to 5%'
This query works and it gives me only age or location data but i want search both values at a same time.
I also tried sub query (Never tried before)
Select y_posts.*, y_meta.*
From wp_posts As y_posts
Inner Join wp_postmeta As y_meta
On y_posts.ID = y_meta.post_id
Where y_posts.post_type = 'download'
And y_posts.post_status = 'publish'
And y_meta.meta_key = 'y_activity_locations'
And y_meta.meta_value Like '%Armidale%'
And (Select yy_meta.* From wp_postmeta As yy_meta Where yy_meta.meta_key = 'y_age'
And yy_meta.meta_value Like '%3 to 5%')
But it gives me this error
1241 - Operand should contain 1 column(s)
So please guide me how can I get two values from same column.
Please give me only query suggestions not any other solutions.
Please try following query :
Select y_posts.*, y_meta.*
From wp_posts As y_posts
Inner Join wp_postmeta As y_meta
On y_posts.ID = y_meta.post_id
Where y_posts.post_type = 'download'
And y_posts.post_status = 'publish'
And (
(y_meta.meta_key = 'y_activity_locations'
And y_meta.meta_value Like '%Armidale%')
Or (y_meta.meta_key = 'y_age'
And y_meta.meta_value Like '%3 to 5%')
)
You try, wordpress query
<?php
$argsCat = array(
'posts_per_page' => -1,
'post_type' => 'download',
'meta_key' => 'y_activity_locations',
'meta_value' => '%Armidale%',
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'y_age',
'value' => '3',
'compare' => '>='
),
'relation' => 'AND',
array(
'key' => 'y_age',
'value' => '5',
'compare' => '<='
)
),
);
$normal_array = get_posts($argsCat);
?>

Paginate do not sort DESC

I can't display Agreements with agreement_number less than 7 and order it by agreement_number DESC.
I have read Pagination CakePHP Cook Book and can't find where my code is wrong. It display only less than 7, but always ASC. I have found similar question here, [that works],(CakePHP paginate and order by) and do not know why. Agreement.agreement_number is int(4).
$this->Agreement->recursive = 0;
$agreements = $this->Paginator->paginate('Agreement', array(
'Agreement.agreement_number <' => '7'
), array(
'Agreement.agreement_number' => 'desc'
)
);
$this->set('agreements', $agreements);
}
Exact cake version is 2.5.2.
... Where did you read that that was the correct syntax?
The paginate function's third parameter is for sorting (and I mean, within the table... with those down and up arrows).
List of allowed fields for ordering. This allows you to prevent
ordering on non-indexed, or undesirable columns.
You have the exact link used for documentation of the API, but you don't seem to be following it (like, from here and here)
$this->Paginator->settings = array(
'Agreement' => array(
'order' => array('Agreement.agreement_number' => 'desc')
)
);
$agreements = $this->Paginator->paginate('Agreement', array(
'Agreement.agreement_number <' => '7'));

Categories