Wp eCommerce meta information search - wordpress

This is a repeat of this question as I get quicker answers on SO.
Out of the box, WP eCommerce does not support Meta search. This is a big problem if you are selling books online and need clients to be able to search by the most important Meta tag, Author!
Can someone please tell me how I could go about including the meta information search or is there a better plugin that does not cost an arm and a leg to search for meta information too?
EDIT:
I have since found a plugin that will do a better search called Relevanssi and Alexander Gieg extended it a little further with his own code:
add_filter('relevanssi_excerpt_content','wpscproductexcerpt',10,3);
function wpscproductexcerpt($content, $post, $query) {
if ( 'wpsc-product' == relevanssi_get_post_type($post->ID) ) {
$content = $post->post_content . ( !empty($post->post_excerpt) ? " $post->post_excerpt" : '' );
if ( 'on' == get_option('relevanssi_expand_shortcodes') ) {
$content = do_shortcode($content);
} else {
$content = strip_shortcodes($content);
}
// The line below fixes a minor bug in Relevanssi 2.9.14 custom excerpt generating code:
$content = preg_replace("/\t/", ' ', $content);
}
return $content;
}
I need to be able to extend this a little further now since I am user Visser Lab's Custom Field's plugin to make up for the deficiencies of WP eCommerce and need to figure out how to include the meta information from this plugin?
Many thanks

From what it turns out, WP eCommerce is using the default WordPress query for displaying it's search results. That's great because this makes it really easy to modify this query to fit our needs. Here is an example code:
function mySearchFilter($query) {
// is_wp_ecommerce_search() is not an actual function - if you know how to check if the search is from WP eCommerce - you might want to do that, so that you don't modify the normal WordPress search
if ( $query->is_search /* && is_wp_ecommerce_search() */ ) {
$query->set('meta_query', array( array( 'key' => 'custom_key', 'value' => $_GET['custom_key'], 'compare' => 'LIKE' ) ) );
};
return $query;
};
add_filter('pre_get_posts','mySearchFilter');
I'm not sure of an exact function for checking whether this is a WP eCommerce search, that's why the && is_wp_ecommerce_search() is commented-out.
You will have to adjust:
'key' => 'custom_key' part - enter the name of your custom field that you'll be looking for.
'value' => $_GET['custom_key'] - enter the value that you'll be looking for. I assume that it will be coming from a $_GET argument, but your case might differ.
'compare' => 'LIKE' - the link bellow will give details about the structure of the meta_query argument, but basically this will look for custom fields with value similar to the searched one.
You can find more details on the meta_query query argument at the Class Reference/WP Query WordPress codex page.

Related

How do I prioritize posts over pages in a wordpress/genesis search result page?

How do I prioritize posts over pages in a wordpress/genesis search result page? Similar to this code, except I'd like posts to show before pages and I can't seem to fully adjust this code to do it:
function change_search_result_order($query) {
// Check if current page is search page
if ($query->is_search) {
// Specify orderby and order, ordering will be by slug of post_type: pAge > pOst
$query->set('orderby', 'post_type');
$query->set('order', 'ASC');
};
return $query;
}
add_filter('pre_get_posts', 'change_search_result_order');
this code block was also mentioned, but I can't seem to find where it fits--if I could, would it be as simple as switching page and post?
$query->set('post_type', array('page','post'));
Original source: How to prioritize pages over posts when searching?
thanks in advance!
Adam
If you break down the code that was given in that other answer, you can clearly see what it's doing.
The custom function is called on pre_get_posts, which is before any posts are fetched.
Inside the function, it makes sure it's a search query, and won't fire on other pages or page templates.
It sets the post_types to page and post, thereby removing any custom post types.
It changes the order of the post to order by the name of the post_type
It changes the order of the posts to be Ascending or Descending.
Add the post_types in there, so that way if you use another plugin that adds CPTs or add your own, they won't be included (such as events or staff members).
Since WP 4.0 the $query accepts type as an orderby parameter. Note that post_type works as well, but the default non-aliased value is type.
Change the order to Desc since you want [PO]sts before [PA]ges
add_filter( 'pre_get_posts', 'change_search_result_order' );
function change_search_result_order($query){
if( $query->is_search ){
$query->set( 'post_type', array( 'page', 'post' ) );
$query->set( 'orderby', 'type' );
$query->set( 'order', 'DESC' );
};
return $query;
}
Stick that in your functions.php file and you should be good to go.

WooCommerce - include custom fields in the search function

So I'm looking to include the functionality of displaying content by its custom fields as well as its title and content.
I need to be able to search for orders, as well as subscriptions, on WooCommerce by custom field as well as the normal method. Is there any way I can, without adding additional search forms or booleans, simply get Wordpress to display posts that match with the search term by their custom fields, too?
I have used the following code thanks to a responder on here:
function custom_search_query( $query ) {
$custom_fields = array(
// put all the meta fields you want to search for here
"gender",
"birthdate"
);
$searchterm = $query->query_vars['s'];
// we have to remove the "s" parameter from the query, because it will prevent the posts from being found
$query->query_vars['s'] = "";
if ($searchterm != "") {
$meta_query = array('relation' => 'OR');
foreach($custom_fields as $cf) {
array_push($meta_query, array(
'key' => $cf,
'value' => $searchterm,
'compare' => '=='
));
}
$query->set("meta_query", $meta_query);
};
}
add_filter( "pre_get_posts", "custom_search_query");
This works great when searching orders, but what I need to search is subscriptions, where it doesn't work.
Help would be much appreciated!

Event Espresso Query Modification?

I am using Event Espresso with WordPress.
May u help me out in further query modification?
Hope you will :)
I want to use meta_query to list events on page.
Somewhat like below code.
$atts = array(
'title' => NULL,
'limit' => 10,
'css_class' => NULL,
'show_expired' => FALSE,
'month' => NULL,
'category_slug' => NULL,
'order_by' => 'start_date',
//'order_by' => 'end_date',
'sort' => 'DESC',
'meta_query' => array(
array(
'key' => 'start_date',
'value' => '2017-01-08 08:00:00',
'type' => 'DATETIME',
'compare' => '>=',
),
)
);
I want to implement search functionality for Event Espresso and i have those fields:
State - Dropdown (How to list all state? May be Venue)
Category - Dropdown
Start Date - Datepicker
End Date - Datepicker
Keyword - input
On submit those values will be submitted and based on those , i will get filtered events that are related with those values.
So how to implement this?
Please help.
Thanks in Advance
A slightly delayed response to this...
First, this depends on the version of EE you're using. They support both EE3 and EE4. I use EE4, so any reference to code I make is specific to version 4.
In order to create an events archive with a filtering functionality, you'd need to use the EE events archive. EE uses custom database tables and a lot of different post types to achieve what you see, so creating a simple archive with these filters won't work very well. Very little is stored in the _posts and _postmeta tables and you need to get post meta from related post types and tables that aren't laid out like WP_Query likes. They have a shortcode for the events list, which is laid out here and has a lot of the filters you're looking for, but no search functionality.
Their support forum has a lot of snippets and such created by their staff and there's a (long) related post about formatting the events archive page here. You'll also need to go through this documentation to see about their custom methods and hooks.
You could copy over and modify the archive templates they describe in a child theme to add search functionality to the top of the page. This would allow you to directly override the archive. You'll need to make use of some fancy filters, which are covered pretty clearly over on WPMU Dev.
The pieces of information you're looking for for filtering are here, minus keyword:
<?php
if( have_posts() ){
while ( have_posts() ){ the_post(); // enter the WordPress loop
$id = get_the_ID(); // get the ID of the current post in the loop, post ID = EVT_ID
$terms = get_the_terms( $id, 'espresso_event_categories' ); // get the event categories for the current post
if ( $terms && ! is_wp_error( $terms ) ) {
$cats = array();
foreach ( $terms as $term ) {
// do something
}
}
$event = EEM_Event::instance()->get_one_by_ID( $id ); // get the event OBJECT using EE's existing method
$venue = $event->venue(); // get the venue object for the current event
$state = $venue instanceof EE_Venue ? $venue->state_abbrev(); // get the event venue's state, but you can use state_name() to get the full name
// using the event to get the first and last date for the entire event. Sub $datetime for $event to do it per datetime
$start_date = $event->start_date('j M Y');
$end_date = $event->end_date('j M Y');
$start_time = $event->start_time(get_option('time_format'));
$end_time = $event->end_time(get_option('time_format'));
?>
<!-- Do some awesome layout stuff here -->
<?php
}
}
espresso_pagination();
?>
This will give you the meta for each post within the loop as variables, but you may want to pull them into pre_get_posts. You can just as easily create an array of $events and then filter it using the variables.
I'm not sure what your exact needs are regarding keyword. Do you mean tags? Title keywords? Description search? You'll need to narrow down exactly what you need this to do in order to write a function for it.

Creating pages from Ninja form data

I've created a WordPress page with a Ninja form on it that collects miscellaneous data about a product, including some uploaded images. The page with the form is accessible from the main menu by clicking the "Input" item, so the user doesn't need to access the backend to upload their product data.
I now want to put this data into a custom post type called "Listing." There will eventually be thousands of these data sets and so thousands of "Listing" pages, as people come to the site, click Input in the main menu to get to the page with the Ninja form and fill it out.
Could someone tell me how they would go about now building these listing pages from the data the form has collected?
I'm running Ninja's Front-End Post option which supposedly will create a page from the form data. This plugin has some Post creation settings where you can select the post type to create, but this isn't working for me. I would expect the submitted form data to show up under dashboard | Listings, but there's nothing there after submitting the form.
Has anyone gotten this to work?
Thanks for your help.
I think you can use only Ninja Forms without extensions, and hook directly in 'ninja_forms_after_submission' that fires after submission and allow you to use data submitted and perform actions.
This is a starter codebase to achieve your result, but needs to be customized on your needs and your form structure.
add_action( 'ninja_forms_after_submission', 'create_page_from_ninjaform' );
function create_page_from_ninjaform( $form_data ){
// your fields data
$form_fields = $form_data[ 'fields' ];
// !!! this is an example, it depends form fields in your form
$title = $form_fields[ 1 ][ 'value' ];
$content = $form_fields[ 2 ][ 'value' ];
$sample_meta_field = $form_fields[ 3 ][ 'value' ];
$new_post = array(
'post_title' => $title,
'post_content' => $content,
'post_status' => 'publish',
'post_type' => 'listing', // be sure this is the post type name
);
$new_post_id = wp_insert_post( $new_post );
update_post_meta( $new_post_id, 'your_meta_key', $sample_meta_field );
}
This code should be copied in functions.php file
Not tested of course.
Good luck ;)
The Ninja Forms Front-end Posting extension isn't really meant for displaying form submission data on the front end.
From: https://ninjaforms.com/extensions/front-end-posting/
"The Ninja Forms Front-end Posting extension gives you the power of the WordPress post editor on any publicly viewable page you choose."
If you want to show Ninja Forms submission data on the front end, you will have to retrieve them from the database with code in functions.php or by writing a plugin (recommended). You could then parse and manipulate them and create a shortcode that would allow you to insert your formatted submission data easily in Wordpress posts or pages.
Here's a link to a feature request, asking for the same thing. The author of that request posted a link to a plugin (click Download as Plugin) they wrote which may do what you want or give you further insights as to how you could implement this.
https://github.com/wpninjas/ninja-forms/issues/892
If you do not mind paying a little money for a plugin I would recommend using gravity forms rather then ninja forms for more advanced stuff like this.
I manually create a custom post type "oproep" and used a gravityforms plugin to create a custom post from type oproep when an user submits the form.
Because you use custom post type archive pages www.mysite.com/oproep will be automatically created so you already have a list of "Listings". The singe pages www.mysite.com/oproep/title will also be created for you by default, you could override these templates as well if you would like depending on your theme.
The only thing you have to do is add a few php lines to your functions.php (or write your own plugin) that adds the custom post type. The rest all works automatically.
I went so far as writing code to make users able to edit their submissions, read custom taxonomy tags in dropdowns etc. You got lots and lots of more options using gravity forms.
FrancescoCarlucci's answer is correct, but just adding an additional comment: in case you want to specify by form field IDs which fields should go where in your post, NinjaForms passes the ID as a number (in my case for example, I needed field 136 for my post title). It may have been obvious but I racked my brain for a while until I figured it out.
function create_post($form_data) {
$form_fields = $form_data[ 'fields' ];
$post_fields = array(
'post_content' => '',
'post_content_filtered' => '',
'post_title' => '',
'post_excerpt' => '',
'post_status' => 'pending',
'post_type' => 'post',
);
foreach ($form_fields as $field) {
$field_id = $field[ 'id' ];
$field_key = $field[ 'key' ];
$field_value = $field[ 'value' ];
if ($field_id == 136) {
$post_fields['post_title'] = $field_value;
}
}
wp_insert_post($post_fields, true);
}

Wordpress- Get custom post type associated with a custom taxonomy

According to the WordPress Codex, the get_post_types() function has a 'taxonomies' parameter, but there is no explanation of exactly how it works.
$post_args = array(
'public' => true,
'taxonomies' => 'genre',
'show_ui' => true
);
$post_types = get_post_types($post_args);
This above code doesn't return anything even though genre is a taxonomy that is registered to the 'book' post type.
If i have a custom post type of 'books' with some custom taxonomies of say, 'author' and 'genre'... I'd like to use get_post_type() to return the post type associated with 'genre'... obviously 'books'. I'm trying to make something that will help set the post_type parameter in get_posts().. since that only searches posts by default and not custom post types. Obviously I could just set the post_type parameter, but I want to make it forward compatible with any future post types I might use in my different themes.
Although I'm a bit late to the party on this one:
taxonomies isn't a valid argument for get_post_types(), so at best it will simply ignore the argument and return a list of all registered post types.
Probably your best bet is the get_taxonomy() object:
$currentTaxonomy = get_query_var('taxonomy');
// Or: $currentTaxonomy = 'genre';
if ($currentTaxonomy) {
$taxObject = get_taxonomy($currentTaxonomy);
$postTypeArray = $taxObject->object_type;
}
This will give you an array of all post types that taxonomy is registered to.
Might want to use a combination of both: get_posts() and then pass in your post_type. Something like below might help get you started.
$post_types = get_post_types();
if ( is_category() || is_tag()) {
$post_type = get_query_var('article');
if ( $post_type )
$post_type = $post_type;
else
$post_type = $post_types;
$query->set('post_type', $post_type);
return $query;
}
}
You would NOT want to use is_category and is_tag, you would use something like is_in_taxonomy(). What exactly are you trying to do? I think you're trying to do the same thing as me which is...
on archive page for custom post type use a custom loop for ALL taxonomies of certain custom post type. like the following:
taxonomy-[MY-CPT].php
vs.
taxonomy-[MY-CUSTOM-TAXONOMY].php
I'm actually trying to do the same as we speak, I'll let you know what I come up with.
NOTE there I just made up the is_in_taxonomy()

Resources