custom search against custom post type - wordpress

In my wordpress site i need two types of search one for basic wordpress search another is custom search.both r separate .first one is ok but second one is creating problem.In custom search i have to search category and key word,Here category is custom_taxonomy and post type also custom post type.
taxonomy=faq-group
post_type=faq-item
example: if any one search category=Australia and keyword=visa Then it will show all post that have visa keyword and Australia category from faq module.
I've searched it in google.I think,i've write custom query
Thanks in advance

Use following code for achieve this
function custom_search_where( $where )
{
if(isset($_POST['your-custom-serchtext'])){
$where = "post_content Like %$_POST['your-custom-serchtext']% ", $where );
}
return $where;
}
$query = new WP_Query( array('post_type' => 'faq-item','tax_query' => array(
array(
'taxonomy' => 'faq-group',
'field' => 'slug',
'terms' => 'australia '
)
)) );
add_filter('posts_where', 'custom_search_where' );
if ( $query->have_posts() ) {
while ( $query->have_posts() ) {
$query->the_post();
echo '<li>' . get_the_title() . '</li>';
}
} else {
// no posts found
}
remove_filter( 'posts_where', 'custom_search_where' );
I have not tested this code but I hope it will useful for you.

Related

How to create a filter to display custom post types

I have a custom post type 'projects' and have an overview page that displays these post types with featured image & post title. I have also created a custom taxonomy for that Post Type and assigned the posts to the categories from that taxonomy.
What I want to achieve now is that on the Overview Page where all the posts are listed, above them should be something like a filter bar with the custom taxonomy categories displayed.
My question now is: What WordPress functions do I need so that when someone clicks on one of the categories, only the posts assigned to that category will be displayed? I don't want the page to refresh or load another page. Here is an example of what I want to achieve: https://www.hauserlacour.de/en/work
Also, I am not a coder. I use Pinegrow to convert my static html sites to a wordpress theme. but in Pinegrow I have the option of a lot of WP function. That's why I just need to understand how the set up of something like above would work.
Many thanks in advance!
If you know a bit more about the the WP_Query you can use the tax_query as below:
$args = array(
'post_type' => 'project',
'post_status' => 'publish',
'tax_query' => array(
array(
'taxonomy' => array( 'project_cat' ), // <-- NO! Does not work.
'field' => 'slug',
'terms' => array( 'project_cat1', 'project_cat2')
)
)
);
$query = new WP_Query( $args );
Reference: https://developer.wordpress.org/reference/classes/wp_tax_query/
or you can simply list the taxonomy terms and simply redirect to taxonomy detail page where respective projects will be listed automatically.
$args = array( 'hide_empty=0' );
$terms = get_terms( 'my_term', $args );
if ( ! empty( $terms ) && ! is_wp_error( $terms ) ) {
$count = count( $terms );
$i = 0;
$term_list = '<p class="my_term-archive">';
foreach ( $terms as $term ) {
$i++;
$term_list .= '' . $term->name . '';
if ( $count != $i ) {
$term_list .= ' ยท ';
}
else {
$term_list .= '</p>';
}
}
echo $term_list;
}
Reference: https://developer.wordpress.org/reference/functions/get_terms/

Hide specific category tag from product page

does anyone know how to hide one specific category tag name from the list in WooCommerce Product page?
Example: Product 1 belongs to Category A, B, C.
I do not want to show category B in tag list on product page. Only A and C.
Thanks in advance for help.
Building on my tutorial on modifying the product query and using the appropriate WP_Query Parameters I think you could do something like the following to exclude all products in the product category with the slug "category-b". You will need to adjust the slug as needed. Untested.
add_action( 'woocommerce_product_query', 'so_31478197_product_query' );
function so_31478197_product_query( $q ){
$tax_query => array(
array(
'taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => 'category-b',
'operator' => 'NOT IN',
),
),
$q->set( 'tax_query', (array) $tax_query );
}
Thank you helgatheviking.
I coded it manually creating a custom function.
I paste it here, so it could be helpful in the future.
function hide_product_cat() {
global $post;
$terms = get_the_terms( $post->ID, 'product_cat' );
if(!empty($terms)){
echo "<span class='prod_categories'>" . _n( 'Category:', 'Categories:', count($terms), 'woocommerce' ) . " ";
foreach ($terms as $term) {
$woo_cat_id = $term->term_id; //category ID
$woo_cat_name = $term->name; //category name
$woo_cat_slug = $term->slug; //category slug
if($woo_cat_id != <MY_CATEGORY_ID_B> ){
$result .= '' . $woo_cat_name . ', ';
}
}
$result = substr($result, 0, -2) . ".";
echo $result;
echo "</span>";
}
}

Wordpress: Custom post type segregation on Category page

I've got two custom post types, for example 'Cars' and 'Bikes'. I've used Wordpress's default category to categorise the posts from the two post types. Let's say for example the categories are 'Red', 'Blue' and 'Black'.
What I'm trying to achieve here is that when I go to the category page for 'Red', I want to see the 'Cars' and the 'Bikes' that are categorised under 'Red'. I'm using the category.php and this is the query that I'm trying to run:
$car_args = array(
'posts_per_page' => -1,
'orderby' => 'date',
'order' => 'DESC',
'post_type' => 'cars',
'post_status' => 'publish',
'cat' => $cat
);
// The Query
$car_query = new WP_Query( $car_args );
// The Loop
if ( $car_query ->have_posts() ) {
echo "<h3>Cars</h3>";
while ( $car_query->have_posts() ) {
$car_query->the_post();
echo get_post_type() . '' . get_the_title() . '<br />';
}
} else {
// no posts found
}
$bikes_args = array(
'posts_per_page' => -1,
'orderby' => 'date',
'order' => 'DESC',
'post_type' => 'bikes',
'post_status' => 'publish',
'cat' => $cat
);
// The Query
$bikes_query = new WP_Query( $bikes_args );
// The Loop
if ( $bikes_query ->have_posts() ) {
echo "<h3>Bikes</h3>";
while ( $bikes_query->have_posts() ) {
$bikes_query->the_post();
echo get_post_type() . '' . get_the_title() . '<br />';
}
} else {
// no posts found
}
/* Restore original Post Data */
wp_reset_postdata();
The $cat in the query gets the category id of 'Red' category. Both these queries are correctly restricting the posts by the 'Red' category but posts from both the 'Cars' post type and 'Bikes' post type are showing up even though I've tried to restrict by post type. I've read in a few articles that Wordpress ignores the post type args on the category page. Is this true and if it is, is there a workaround for this?
What you are trying to do is possible with one query only, and only with the main query without any custom queries.
First of all, lets first add your custom post types to your category page. By default, custom pist types are excluded from category pages. So we need to add this manually to the main query arguments via pre_get_posts. Add the following to your functions.php: (CAVEAT: Untested and also requires PHP 5.3+)
add_action( 'pre_get_posts', function ( $q )
{
if ( !is_admin() && $q->is_main_query() && $q->is_category() ) {
$q->set( 'post_type', array( 'post', 'cars', 'bikes' ) ); // Change this according to your post types
$q->set( 'nopaging', true ); // This will get all posts, same as posts_per_page=-1
}
});
You should now have all posts from a clicked category is your set post types on your category pages.
Next, wee need to sort out your loops. Delete all your custom queries in category.php and replace it with the default loop. As you would want two block ordered by post type, we will use rewind_posts() so we can run the loop twice
if ( have_posts() ) {
while ( have_posts() ) {
the_post();
if ( $post->post_type == 'cars' ) { //Change accordingly to only show cars in this loop
// Your loop
}
}
rewind_posts();
while ( have_posts() ) {
the_post();
if ( $post->post_type == 'bikes' ) { // Change accordingly to only show bikes
// Your loop
}
}
}
This should now display your posts in two block sorted by post type

Contact Form 7 and Custom post type

I want to use contact form 7 in Wordpress to build a order Form. I want the content of the order Form to be populated with content from a custom post type "trade Show Material" - The post type contains the fields "name" "number" "description" "photo" . The idea will be that each piece can be selected from the form . Can anyone offer the general direction for this? Should I perhaps be using another plugin entirely?
Maybe you can use the wpcf7_form_tag filter hook for this.
If you want to use a custom post type as the options of a dropdown (select) you can add something like the example below in your functions.php:
function dynamic_field_values ( $tag, $unused ) {
if ( $tag['name'] != 'your-field-name' )
return $tag;
$args = array (
'numberposts' => -1,
'post_type' => 'your-custom-post-type',
'orderby' => 'title',
'order' => 'ASC',
);
$custom_posts = get_posts($args);
if ( ! $custom_posts )
return $tag;
foreach ( $custom_posts as $custom_post ) {
$tag['raw_values'][] = $custom_post->post_title;
$tag['values'][] = $custom_post->post_title;
$tag['labels'][] = $custom_post->post_title;
}
return $tag;
}
add_filter( 'wpcf7_form_tag', 'dynamic_field_values', 10, 2);
In your form you can add the field:
[select* your-field-name include_blank]
In the example above the post_title is used in the options of the dropdown. You can add your own fields here (name, number, description, photo).
I do no think the wpcf7_form_tag works in the same way as vicente showed in his great answer before. It may have changed since 2015.
If you read here it explains how you need to use the wpcf7_form_tag: https://contactform7.com/2015/01/10/adding-a-custom-form-tag/
With that in mind along with this other post from Contact Form 7: https://contactform7.com/2015/02/27/using-values-from-a-form-tag/#more-13351
I came up with this code to create a custom dropdown list for a custom post type that I have.
add_action( 'wpcf7_init', 'custom_add_form_tag_customlist' );
function custom_add_form_tag_customlist() {
wpcf7_add_form_tag( array( 'customlist', 'customlist*' ),
'custom_customlist_form_tag_handler', true );
}
function custom_customlist_form_tag_handler( $tag ) {
$tag = new WPCF7_FormTag( $tag );
if ( empty( $tag->name ) ) {
return '';
}
$customlist = '';
$query = new WP_Query(array(
'post_type' => 'CUSTOM POST TYPE HERE',
'post_status' => 'publish',
'posts_per_page' => -1,
'orderby' => 'title',
'order' => 'ASC',
));
while ($query->have_posts()) {
$query->the_post();
$post_title = get_the_title();
$customlist .= sprintf( '<option value="%1$s">%2$s</option>',
esc_html( $post_title ), esc_html( $post_title ) );
}
wp_reset_query();
$customlist = sprintf(
'<select name="%1$s" id="%2$s">%3$s</select>', $tag->name,
$tag->name . '-options',
$customlist );
return $customlist;
}
Then you use the tag in contact form 7 like this.
[customlist your-field-name]
Hopefully this helps someone else who was looking for a way to do this like I was.
You could alter it to get any information you need from the custom post type.
It does not have any validation though.
Clyde Thomas code still works nice, thanks !
In my case I need the data from a plugin instead of a post so I've modified the code removing the WP_query and the while
global $wpdb;
$result = $wpdb->get_results("SELECT title FROM wp_asl_stores ORDER BY title ASC ");
foreach($result as $row) {
$customlist .= sprintf( '<option value="%1$s">%2$s</option>',
esc_html( $row->title ), esc_html( $row->title ) );
}

Advanced custom fields - relationships logic reversed

On our site we currently have 'work' posts that we create and then associate with an author and genre post type.
The overall goal is:
When we view an author or genre post we want to list all the work posts that we have associated/related with that certain author/genre.
We are using the following code which seems to have us half way...
<?php $args = array(
'numberposts' => -1,
'post_type' => 'post',
'meta_query' => array(
array(
'key' => 'related_posts',
'value' => $post->id
)
)
);
$posts_array = get_posts( $args );
var_dump($posts_array);
if( $posts_array ) {
echo '<ul>';
foreach( $posts_array as $related ) {
echo '<li>';
echo '' . $related->post_title . '';
echo '</li>';
}
echo '</ul>';
}
?>
However the 'value' field in the array doesn't work. It technically should be passing the id of the current post (author or genre) and selecting the related content. When we remove this from the array it does bring all the posts in whether they are related or not.
In summary, we think that the 'value' problem may be the key in resolving the issue as that is what should be filtering the posts.
Thanks in advance
did you try $post->ID? (uppercase not lowercase)

Resources