search with all field including custom field wordpress - wordpress

I use custom fields for lots of my content on both Posts and Pages, so that half of the text will be in the main post body and the other half in a custom field.
Unfortunately, when doing this only the text in the post body is searchable.
I want to have all the custom fields included in the search (just joined together as one). Preferably without installing a plugin, as all the plugins I have seen do not seem to be able to do this or they add loads of options I don't need.
I just would like one search box, that uses WordPress's default search but finds words in custom fields as well as those in the main body.
Is this possible?

you can do this by meta_query:
Try something like
$args = array(
'post_type' => 'custom_post',
'posts_per_page' => 10,
'order' => 'ASC',
'meta_query' => array(
'relation' => 'OR',
array(
'key' => 'custom_filed1',
'value' => $_REQUEST['custom_filed1'],
'compare' => 'LIKE'
),
array(
'key' => 'custom_field2',
'value' => array( $_REQUEST['custom_field2'], $_REQUEST['custom_field2'] ),
'type' => 'numeric',
'compare' => 'BETWEEN'
)
);
$wp_query = new WP_Query( $args );
//echo $wp_query->request;
while ( $wp_query->have_posts() ) : $wp_query->the_post(); ?>
//DISPLAY POST HERE
may this help you

Related

How to query 'classic' posts with custom field from ACF (relation field multi values)

I need your expertise regarding a problem I have with wordpress.
Context :
I have :
Classic posts | posts
ACF | custom field applied to posts named: thematique_lien
which is a relationship field with a custom type named: Thematic
see the screen below :
My problem is that I can't make a query that retrieves :
all the posts that have in the custom field 'thematique_lien' the text 'XXX
I'm not sure if this is a good idea, but I'm not sure if it's a good idea: Value1, Value2...
Here are my query tests:
$args = array(
numberposts' => -1,
post_type' => 'post',
'meta_key' => 'thematic_link',
meta_value' => 'jobs'.
);
#OR#
$args = array(
post_type' => array('post'),
posts_per_page' => -1,
'meta_query' => array(
'key'=> 'theme_link',
value' => 'jobs',
compare'=> 'LIKE'
) )
);
I've tried all over the place, but nothing works, if anyone has any ideas?
Thanks to you and have a nice day/evening ;)
You could try this example based on the ACF docs:
$doctors = get_posts(array(
'post_type' => 'doctor',
'meta_query' => array(
array(
'key' => 'location', // name of custom field
'value' => '"' . get_the_ID() . '"', // matches exactly "123", not just 123. This prevents a match for "1234"
'compare' => 'LIKE'
)
)
));

Wordpress get posts[relationship] from users

I have a case in which I have 2 custom post types, let's say they are projects and teams. Using Advanced custom fields, I have a relationship field while creating a project and I'm assigning which teams are working on it. (Important: there is no custom field in the teams post type). Later on, I want on single-team, to list all the projects that this team was working on.
The example that Advanced custom fields has is where you have a custom field in the team, not in the project, and I need to do the opposite. (Here is how acf documentation is https://www.advancedcustomfields.com/resources/querying-relationship-fields/).
I tried doing this, but it doesn't work, it says that I don't post a correct data.
$team_id = get_the_ID();
$posts = get_posts(array(
'post_type' => 'projects',
'orderby' => 'teams',
'post__in' => $team_id,
));
Since the ACF relationship field is located in your Project custom post type in your single-team.php file where you want to list of the projects on which the team member worked on you can do the following:
$args = array(
'post_type' => 'PROJECTS CPT NAME HERE',
'post_status' => 'publish',
'posts_per_page' => 3,
'orderby' => 'date',
'order' => 'DESC',
'meta_query' => array(
'relation' => 'OR',
array(
'key' => 'ACF RELATIONSHIP FIELD NAME HERE',
'value' => get_the_ID(), // the ID of the member
'compare' => 'LIKE',
)
)
);

How to link custom field to shortcode?

I'm trying to link a custom field to a custom shortcode so the shortcode shows displays only the posts with the custom field selected to it.
This is my code below for my shortcode as you can see the key is my custom felid "flash_deal". When I enter the shortcode I just get all the perk psots and no the custom field perk posts?
add_shortcode('foundry_flash', 'shortcode_query_flash');
function shortcode_query_flash($atts, $content){
extract(shortcode_atts(array( // a few default values
'post_type' => 'perks',
'posts_per_page' => -1 ,
'meta_query' => array(
array(
'key' => 'flash_deal', // name of custom field
'value' => '"yes"', // matches exactly "red"
'compare' => 'LIKE'
)
)
), $atts));
The code you show is simply setting up variables - not running any queries at all (see extract documentation & shortcode_atts documentation).
I assume you have more code you just didn't add here, so likely the problem is the double quotes around yes that are causing issues. It's literally looking for "quoteyesquote".
As a general working example to get "Parks" Post based on custom meta, you need to use WP_Query as in the docs here.
$args = array(
'post_type' => 'parks',
'posts_per_page' => -1,
'meta_query' => array(
array(
'key' => 'flash_deal',
'value' => 'yes',
'compare' => 'LIKE',
),
),
);
$query = new WP_Query( $args );
Then use a Nested Loop setup to loop through the results.

Woocommerce product filter meta key

I have built a search a form at custom page in wordpress and want to filter product on shop page using meta keys that are already exists into posts table.
Initially I have tried to filter categories at form page like this but it doesn't work.
$meta_query = array(
'key' => '_years',
'value' => '2009'
);
$args=array(
'meta_query' => $meta_query,
'posts_per_page' => 10,
'post_type' => 'product',
'orderby' => $orderby,
'order' => $order,
'paged' => $paged
);
wc_product_dropdown_categories($args);
The meta_query parameter must be change in an array of array for a single custom field handling:
$meta_query = array(
array(
'key' => '_year',
'value' => '2009',
'compare' => '>',
)
);
Some details from WP_Query page in the Single Custom field handling part
Hope it will work with this.

Wordpress output links to previous and next posts from custom query

I'm using the advanced custom fields plugin for wordpress to create a group of custom post types that have a date set within them.
I'm trying to show the previous post, and the next post, based on the date stored in the custom field. The links need to link to posts that have a date set in the future (so don't show links to posts with dates that have gone by)/
I can get a list of all the posts that are in the future, and out put these using the following code;
<?php
$rightnow = current_time('Ymd');
$args = array(
'post_type' => 'Courses',
'posts_per_page' => '25',
'meta_query' => array(
array(
'key' => 'date_of_the_course_single_day',
'compare' => '>=',
'value' => $rightnow,
)
),
'meta_key' => 'date_of_the_course_single_day',
'orderby' => 'meta_value',
'order' => 'ASC',
'post_status' => 'publish'
);
$posts = get_posts($args);
foreach ( $posts as $post ) {
?>
Output details of post here....
<?php
}
?>
What I thought I could do, is the get the current post's position in the array, to then get details of the posts one before and one after... but I haven't got a clue how to do this.
I've experimented with the wordpress next_post_link and previous_post_link functions, but these seem to work based on when the post was added to wordpress, rather than based on my custom date field.
Am I going about this the complete wrong way? Any tips or pointers would be much appreciated!
Use WP_Query plus paginate_links
$rightnow = current_time('Ymd');
// Query Args
$args = array(
'post_type' => 'Courses',
'posts_per_page' => '25',
'meta_query' => array( array(
'key' => 'date_of_the_course_single_day',
'compare' => '>=',
'value' => $rightnow,
) ),
'meta_key' => 'date_of_the_course_single_day',
'orderby' => 'meta_value',
'order' => 'ASC',
'post_status' => 'publish'
);
$query = new WP_QUery( $arg );
$posts = $query->get_posts();
// Paginate Args
$page_args = array(
'base' => 'your_custom_page_url'.'%_%', // Make sure you got this current depending on your setup
'format' => '/%#%', // requires pretty permalinks
'total' => $query->max_num_pages,
'current' => 0,
'prev_text' => __('«'),
'next_text' => __('»'),
);
foreach ( $posts as $post ) {
// Output
}
echo paginate_links( $page_args );
You have to verify that the base and format of paginate args are correct of it won't properly worked.

Resources