Create Advanced search with taxonomies - wordpress

as always after struggling my mind for 3 days with a problem i decide to ask for help here.
Im trying to create an advance search page with taxonomies and keyword field. You can see what i actually got in http://alianzasuperior.com/empleo/busqueda-avanzada/ ( dont panic, its spanish but just think as taxonomies and you will be ok)
The keyword part is working fine. I can also create taxonomies pages for example taxonomy-job_type.php to handle an individual taxonomy and it works aswell.
The problem is when i try to search in multiple taxonomies , and multiple terms for those taxomies. I tried to figure it out with scribu plugin "query multiple taxonomies" but im not able.
Someone did something similiar for what im trying to do?
Any help or clue will me much appreciate

Like you I am also digging in depth of wordpress multiple search,
check out my question in wordpress stackexchange, it might help you.
Till yet I also had no success in it.
https://wordpress.stackexchange.com/questions/27158/wordpress-multiple-category-search

like other times, after posting here i found my own question. Maybe its not the smarter solution but its working for me.
Im using the new tax_query as commented in http://www.wpmods.com/query-multiple-taxonomies-in-wp-3-1/
Basically if i get 2 taxonomies with two terms for example
$job_type='full-time+free-lancer';
$job_cat='designer+programmer';
I do the following:
$custom_query=false;
$myquery['tax_query'] = array( 'relation' => 'AND');
if ($_GET['job_type']){
$job_type=explode('+',$_GET['job_type']);
foreach ($job_type as $k => $name){
$job_types[]=$name;
}
array_push($myquery['tax_query'],array('taxonomy' => 'job_type','terms' =>$job_types,'field' => 'slug' ,'operator' => 'IN'));
$custom_query=true;
}
if ($_GET['job_cat']){
$job_cat=explode('+',$_GET['job_cat']);
foreach ($job_cat as $k => $name){
$job_cats[]=$name;
}
array_push($myquery['tax_query'],array('taxonomy' => 'job_cat','terms' => $job_cats,'field' => 'slug','operator' => 'IN'));
$custom_query=true;
}
And then if i got more than one term i use the custom query:
if($custom_query) query_posts($myquery);
If you want to mix it with keyword search just do:
$myquery['s']= $yourKeywordVar;
Hope that helps, I just discovered and so far my tests are working fine

Related

Custom function added to Drupal core removed by upgrade

I've recently upgraded a site to Drupal 7.59 with install profile:
Commerce Kickstart (commerce_kickstart-7.x-2.54)
Previously there was a function that had been added to the core which has now been removed because of the upgrade. This shouldn't have been added to the core and I'm not sure why it was. I've added this function back in and its not doing what it did previously so I'm not sure what other changes I would need to make to get it to work.
Here's the function which is found in /profiles/commerce_kickstart/themes/commerce_kickstart_admin/template.php -
function commerce_kickstart_admin_commerce_price_formatted_components($variables) {
// Add the CSS styling to the table.
drupal_add_css(drupal_get_path('module', 'commerce_price') . '/theme/commerce_price.theme.css');
// Build table rows out of the components.
$rows = array();
foreach ($variables['components'] as $name => $component) {
$rows[] = array(
'data' => array(
array(
'data' => $component['title'],
'class' => array('component-title'),
),
array(
'data' => $component['formatted_price'],
'class' => array('component-total'),
),
),
'class' => array(drupal_html_class('component-type-' . $name)),
);
}
if($variables['components']['discount']['price']['amount']){
unset($rows[0]);
unset($rows[2]);
}else{
$rows = array_splice($rows, 2);
}
return theme('table', array('rows' => $rows, 'attributes' => array('class' => array('commerce-price-formatted-components'))));
}
Can anyone give any pointers as to how to get this working? It doesn't appear to even be getting invoked.
Additional info from the comments:
it's a function in the profile?
yes
Was the function added afterwards (as in "Never hack core")?
Yes, looks like it.
Or was it removed by the maintainers?
Doesn't look like this was ever part of any official release
Do you use some version control system like Git?
Yes. This function was added on 14/05/2015 12:18 according to the repo.
Have you checked the profile's release notes and issue queue?
Had a look but don't see anything.
Thanks for adding the extra info!
Well, if this really was custom code than it should never have been added to the profile in the first place. Never ever add custom code to any core or contrib file. As it's going to be deleted as soon as you update. Like it has happened to you.
I guess the most important part of this custom function was drupal_add_css(drupal_get_path('module', 'commerce_price') . '/theme/commerce_price.theme.css'); and that this commerce_price.theme.css maybe also got deleted.
Apart from that it's hard to tell from far and I'm not an expert in the commerce module. So, what I would do now is to narrow down the issue systematically.
Restore your repo to a time in history before this module got updated and get the site running.
Find out what this code is doing exactly, what other functions or flows are involved. Maybe with the help of the Devel module and the mighty dpm() function.
Try to rebuild the custom code from the profile in a custom module.
Then reset the repo to the current state and see if your custom module's code is still firing. If not, debug it to match the updated profile's code.
Apart from that, find the person who added the code and ask them why and what this code is doing. And tell them to never ever again hack core or contrib code :)
Good luck!

Search outside of the wordpress “loop”

I am creating a blog using only Wordpress's backend. I have found functions to get latest posts (wp_get_recent_posts) and all the required data I need. I do this by including wp-load so I have access to WP's functions.
However I cannot find anything that allows me to perform a search outside of Wordpress's theming loops as I have for the rest of the data.
I was hoping there was a search function where I can pass it a search query that could be in title, body content or tag name.
Am I missing something blindingly obvious in the documentation, there seems to be a function for everything else I need outside of WP's "loop".
Does that work for you?
// query for a given term ("search_term")
$search_query = new WP_Query();
$search_posts = $search_query->query('s=search_term');
Source
Answered by sanchothefat:
You can use get_posts() with a search parameter:
$results = get_posts( array( 's' => 'search term' ) );
https://wordpress.stackexchange.com/questions/74763/search-outside-of-the-loop/74766#74766

Getting the X posts BEFORE the last 5 using query_posts

This is almost certainly more simple than I assume it to be, but I'm beating my head against the wall here... I've got the following code that simply grabs the most recent 5 posts. What I'd like to do is create an archive that shows the X number of posts that were posted BEFORE the last 5, but I'm having some trouble coming up with the right query_posts variables to do it. Is there any simple way to do this... or should I just skip the first 5 posts that query_posts returns? that seems like the simple way but I thought this might be an opportunity to learn something about query_posts...
query_posts(array(
'posts_per_page' => 5,
'post_status' => 'publish'
));
You're probably looking for the 'offset' function - see this post:
Wordpress get range of posts

Accessing values in Drupal's $form_values from a custom Drupal Form

EDIT: Seems like my "array-crawling" skills were not enough, thanks for the suggestions.
Moreover, I found out that I was checking the $discounttype condition with a plain "=" instead of a double "==". I guess banging your head on the same block of code for 3 hours makes you dumb and miss the most obvious errors.
First thing first, I'm on Drupal 6.
I have created a form with the following markup:
$form["cart_".$index] = array(
'#type' => 'image_button',
'#src'=> 'files/imghome/sidebar-add-demo.gif',
'#attributes' => array('rel' => '#item', 'class' => 'buybutton', 'title' => $discounttype),
'#prefix'=>'<p class="renewprop">'.$newren.' for '.$node_abb->field_tipo_abb_value.':</p><p class="renewblock"><span class="pricetag">'.$node_abb->field_prezzo_value.''.$discounttype.'</span>',
'#suffix' =>'</p>' ,
'#submit' =>array('usercp_form_submit'),
);
The form renders correctly, as you can see from this picture: http://cl.ly/3D2C2h1t1m2B351L1T31
(the N and R values beside the € symbol are actually the value of the $discounttype variable, just for checking it)
Each white box is basically an istance of the beforementioned form.
I need to pass the value of the $discounttype variable on each submit, so I decided to set it as the title of the submit button.
My problem is that in the submit function itself I cannot access the value of the 'title' attribute contained in the #attributes array. Mainly because probably I don't know the right syntax.
So far I've tried
$foo = $form_values['attributes']['title'];
$foo = $form_values['#attributes']['title'];
$foo = $form_values['attributes']['#title'];
And every other possible combination, but probably I'm just doing it wrong.
It's actually an hour that I'm crawling the web searching for an asnwer but I came up with anything.
first, you should mention form element ID.
so, you can access submit button by $form_state["cart_".$index]['#attributes']['title'];
but actually, why don't you use hidden field ('#type' => 'hidden') ?
I believe you have to use $form_state instead of $form_values. Give this a try:
$foo = $form_state['clicked_button']['#attributes']['title'];
I recommend using the Devel module while developing for Drupal. It is an extremely helpful tool during development, allowing you to see all the queries run when a page loads, stop a redirect to debug, and much more.

Most efficient way to have a 50 state drop down box in Drupal Forms

I know that one way is to have a table in database with all the states and then you would read it in your form. Is there any easier way in your opinion guys ?
I feel bad asking for something like this since it is so elementary however I would suppose something as simple like this would already be implemented in Drupal.
No need to hit the database. Build yourself a function that returns an array of the states.
$form['state'] = array(
'#type' => 'select',
'#options' => mymodule_states_list(),
'#title' => t('State'),
);
function mymodule_states_list() {
return array(
'AL' => 'Alabama',
'AK' => 'Alaska',
...
'WY' => 'Wyoming',
);
}
If you're building the form using Drupal's FormAPI you could just include the array of states in your module code since the names and abbreviations shouldn't be changing any time soon.
If you're trying to keep your code clean, you could save an array of states as a Drupal variable using variable_set() and then retrieve it using variable_get(). You really shouldn't need to bother with the database for that kind of thing.
That is one way to do it, sure. You can store a list of states as a variable, and call it.
$options = variable_get('mymodule_us_states', 0);
So long as it's an array. You could also have an internal function that returns the states.
Or store it in a flat file and read it in. Eg
Flat file =
$us_states_options = array(
'AL' => 'Alabama',
'AK' => 'Alaska',
//...etc
)
Function:
include_once(drupal_get_path('module', 'module_name') .'/us_states.inc');
All pretty ugly, but you at least can edit that file independently, and may work well if you have a larger list. Theres a million ways you could have the list included - using exec, fgetcsv/file, etc....
But I think ceejayoz solution is the nicest. I'd probably spin out those sorts of utility functions into a seperate include to keep it clean myself.
The Country codes API module also provides a Regions codes API module which include US states. With this module, you can get an array suitable for a Form API select element by calling regions_api_iso2_get_options_array('US').
Just remembered this question as I was experimenting with the Geonames module. Among tons of other features, Geonames includes the function "geonames_us_states()" which returns an array of U.S. states keyed by the states' two letter code.

Resources