How to consider custom taxonomy names too on a wordpress search? - php

I'm trying to change wordpress default search behavior in order to consider taxonomies names too. If I inform on the search input the value 'Cybercops', I want the wordpress search to look for posts that have the cybercops word.
But the blog have a custom taxonomy named Heros, and one of the Heros categories could have the name 'Cybercops' too. So, If a post or custom post belongs to these 'Cybercops' Heros category, this posts has to appear on the result of the search.
So far, I have this, because I want to bring future post too:
function my_theme_my_custom_search( $query ) {
if( $query->is_main_query() && $query->is_search() && !is_admin()) {
$query->set( 'post_status', array('publish, future') );
}
}
add_action( 'pre_get_posts', 'my_theme_my_custom_search' );
I tried to use one more $query->set using tax_query but it doesn't work. Can someone help me?

You can use any of the arguments from the WP_Query.
Source # https://developer.wordpress.org/reference/classes/wp_query/
Parameter
Description
tax_query
(array) Use taxonomy parameters (available since version 3.1)
<?php
$tax = array(
array(
'taxonomy' => '_your_custom_taxonomy_',
'field' => 'slug',
'terms' => '_your_custom_term_slug_',
)
);
$query->set( 'tax_query', $tax );
?>

Related

Show only custom post type posts in RSS feed

There are tons of ways of adding custom post types to the RSS feed, but I can't seem to find a way to add CPT UI plugin custom post types to CUSTOM POST TYPE exclusive RSS feed to only query these posts.
Is there way to do this?
Give it a try:
function cptui_add_post_types_to_rss( $query ) {
// We do not want unintended consequences.
if ( ! $query->is_feed() ) {
return;
}
// Replace these slugs with the post types you want to include.
$cptui_post_types = array( 'my_post_type', 'my_other_post_type' );
$query->set(
'post_type',
array_merge(
array( 'post' ),
$cptui_post_types
)
);
}
add_filter( 'pre_get_posts', 'cptui_add_post_types_to_rss' );

Search within selected category in WordPress

I need to alter the search behavior of a WordPress site where the theme doesn't use searchform.php at all (it's the Divi theme). I'd rather not employ a plugin to do this as the site has many plugins already.
The default state of the page is to show "all posts from all categories" (/?cat=0) with a category list dropdown generated using wp_dropdown_categories() that allows the user to select other categories.
The desired use case is:
The user selects a category from a dropdown
The browser goes to the selected category's archive
The user types a search query into the search box on the category archive with the intent of seeing only posts and pages in that category that contain the search term.
I figured I could use the pre_get_posts hook to do this. So I did the following:
function search_by_cat()
{
global $wp_query;
if ( is_search() ) {
$currenturl = add_query_arg( $wp->query_vars, home_url( $wp->request ) );
$previousurl = wp_get_referer();
if ( strpos( $previousurl, '/category/' ) !==false ) {
$searchcategoryurl = untrailingslashit( $previousurl ) . $currenturl;
wp_redirect( $searchcategoryurl );
exit();
}
}
}
add_action('pre_get_posts', 'search_by_cat');
But this just results in ERR_TOO_MANY_REDIRECTS in the browser. I think it might be because the user is submitting a search that results in the same destination as the referrer. How do I break out of the redirect loop so I can (essentially) redisplay the page of results? Or is my entire approach all wrong? Is there a better way?
Here's the general idea from my comments, you'll need to change the CPT and taxonomy types to match your code. Tax queries are a little weird because of the nested array syntax. The comments should hopefully help understand it more.
add_action(
'pre_get_posts',
static function (WP_Query $query) {
// We only want to run on the main search query
if (!$query->is_search() || !$query->is_main_query()) {
return;
}
// Attempt to get the parameter, or default to 0
$category_id = (int)($_GET['category_id'] ?? 0);
if (!$category_id) {
return;
}
// Since we're search by a taxonomy, we probably want to bind it to a type, too
$query
->set(
'post_type',
// Change as needed
'events'
);
$query
->set(
'tax_query',
[
// See this for specifics: https://developer.wordpress.org/reference/classes/wp_query/#taxonomy-parameters
[
// Change as needed
'taxonomy' => 'event-type',
'field' => 'term_id',
'terms' => [$category_id],
],
]
);
}
);

Fixing custom query for wordpress/elementor with Advanced Custom Fields relationship field post types

I was wondering if someone could help me out.
I can't seem to get this case working (since I am a noob in PHP).
Case:
My wordpress site has two 'post types':
Webinar
Spreker (speaker in dutch)
Via Advanced Custom Posts i've set up a relationship field in the webinar post type. So I can pick the speakers related to that particular post.
I use elementor and need to use the elementor's post widget to display the related speaker(s) via the single webinar page template. This has to be done by a custom query. In elementor you can set a custom query ID. This calls for a php query code.
Now I can't seem to get this query array code right.
Elementor documentation about custom queries:
https://developers.elementor.com/custom-query-filter/#Using_the_Custom_Filter
Some related answers on internet (but can't still seem to get it working):
Custom Query Filter for Elementor Posts by relationship field (ACF)
The custom query ID i use in Elementor is 'Spreker_filter'
My code:
add_action( 'elementor/query/spreker_filter', function( $query ) {
// Get current meta Query
$meta_query = $query->get( 'meta_query' );
// If there is no meta query when this filter runs, it should be initialized as an empty array.
if ( ! $meta_query ) {
$meta_query = [];
}
// Append our meta query
$meta_query[] = [
'key' => 'sprekers',
'value' => '"' . get_the_ID() . '"',
'compare' => 'in',
];
$query->set( 'meta_query', $meta_query );
} );
It looks like i was having the same problem.
I've resolved it with this custom query
add_action( 'elementor/query/retrieveLinkedCpts', function( $query ) {
$ids = get_field( 'my_relationship_field', false, false );
$query->set( 'post__in', $ids );
});
source : https://github.com/elementor/elementor/issues/4916
Let me know if it works

How to stop display in search results custom post type in WordPress

I have multiple custom post types. I need display country and location results. but currently show all post types results in search.php page. I need hide results for these post types post ,page, experience, trip-theme.
I m using default theme WordPress Seventeen theme
I have no idea how can do this. please help me.
Thank you.
Try this code
add_action( 'pre_get_posts', 'search_in_custom_post_type' );
function search_in_custom_post_type( $query ) {
if ( ! is_admin() && $query->is_main_query() && $query->is_search() ) {
$query->set( 'post_type', array( 'country', 'location' ) );
}
return $query;
}
When you register post type set one extra parameter for search.
FOR custom post type.
'exclude_from_search' => true,
https://codex.wordpress.org/Function_Reference/register_post_type

WP_Query from searching post except and not page title?

I have a custom post type named 'Company' and a basic search on the top right of the screen. Unfortunately, when submitting a basic search the search results populate companies instead of actual pages such as Resources or Tech Advisory Group. http://labbureau.wpengine.com/
I have modified my custom query to search only for pages and ignore all company posts however, the basic search is still displaying companies instead of the actual TITLES of the pages. What can be done of this?
Below is my custom query:
$keyword = $_GET['s'];
$wp_query = new WP_Query(
array(
's' => $keyword,
'post_type' => 'page',
'tax_query' => array(
'taxonomy' => 'company',
'operator' => 'NOT IN'
),
'orderby' => 'title'
)
);
Start of loop:
<?php if ( $wp_query->have_posts() ) while ( $wp_query->have_posts() ) : $wp_query->the_post(); ?>
Try add hidden input in the search form with the post type you want to make the search on:
<input type="hidden" name="post_type" value="post_type_name" />
On your theme functions.php use this code
function searchfilter($query) {
if ($query->is_search && !is_admin() ) {
$query->set('post_type',array('page'));
}
return $query;
}
add_filter('pre_get_posts','searchfilter');
Here is a simple solution. Go to your Media Gallery and do a search for a file. If the search title is the same as the image title and no results display you have a conflicting query that is running rampant. More than likely this can be found in the functions.php file and is represented as a filter that looks like the following:
function search_filter( $query ) {
if ( $query->is_search ) {
$query->set( 'post_type', array('company') );
}
return $query;
}
add_filter('pre_get_posts','search_filter');
The function.php file is the king of the hill and will take over all other custom post types you have on your website. If you have multiple searches they will both search for the company post type regardless of whether or not you create a new WordPress query in a template. You need to comment out add_filter. In this example, you need to create a conditional statement for two different search loops. One of your search templates (HTML) needs to have a hidden input that can be retrieved by one of your conditional points to reference a new template with a new query. Do not try the conditional statement with custom post types because you will have to change this in more than one place if it needs to be changed. Here is an example of what this conditional would look like:
if(isset($_GET['search_simple']) && $_GET['search_simple'] = 'search' && !isset($_GET['hdr-search']))
{
get_template_part('loop-search');
}
else if(isset($_GET['search_simple']) && $_GET['search_simple'] = 'search' && isset($_GET['hdr-search']))
{
get_template_part('loop-search2');
}
else
{
get_template_part('loop-search-blog');
}

Categories