Hello @hube2,
I followed your advice and I did a query directly inside my archive template with your 3rd choice.
And it works like a charm.
You’re the best.
Thank you so much !!
All the best.
Pierre
When it comes to WC I generally try to use product attributes instead of ACF fields and then use existing search plugins for searching products.
There are plugins available that will can be used to include custom fields in the WP keyword search.
I usually go a different path when it comes to keyword searching and ACF.
but for creating a search field that specifically searches a single field by keyword, I have not seen anything like this, all I can do is point you in the right direction. Like I said, you need to split the input into words on spaces and punctuation, I would probably try something simple like
$parts = preg_split('/\W/1', $string)
This will split up a string on any non word character. Then you build the query that does a LIKE query on each for each part. It would not be perfect, but it would get the job done.
I am having the same issue and I notice that only happens whenever I use wording like SQL query keywords such as select and delete. Have anyone found the solution? I tried the ModSecurity method and it didn’t work for me.
Sorry late answer, I have added a beaver builder HTML module with the code I mention in last comment. Together with some Javascript lines, they are displaying this icon: https://share.getcloudapp.com/yAuQYXkr
The problem is pointing to a custom field inside the HTML.
North Indian hill resort Mussoorie, often known as the “”Queen of Hills,”” is a convenient, affordable, and picturesque destination. There is no set Best time to visit Mussoorie, but it has grown to be the most well-known destination for year-round tourism with family and friends. With magnificent locations all around, Mussoorie is the ideal destination if you want to unwind and spend some time amidst the natural beauty.
You will find some jewels as you head to Mussoorie, and a few will be found as you reach between the lush green hills wrapped with thick layers of snow in the prime season. But to see the mountains with white snow, you should prearrange the best time to visit Mussoorie to see snowfall.The weather in Mussoorie can vary, as was already said. When the sky is clear, summers are lovely. The monsoon provides the ideal setting for exploring the hills. Since there is a lot of snow in Mussoorie throughout the winter months of November to February, this is considered the peak season.
Thank you for your reply. Though not much help to my situation I would caution you on not using add_editor_style(). if you enqueue your css with a different method, your styles will not be applied to previews like blocks and patterns (maybe even style previews though I haven’t confirmed this).
Ideally we should be able to exclude an element from a page. Like “apply these styles to this page but ignore all divs with the class of .acf-block-component” something like:
:not(.acf-block-component) {
@import "links"
... ect ...
}
// in the editor this would output something like:
.editor-styles-wrapper :not(.acf-block-component) a { color: blue }
naturally this will not work. and since there does not seem to be an easy way around this. I’d propose ACF be much more specific with their block editor styles even at the risk or breaking the WordPress branding. I’d rather see a ACF themed block editor than a busted one that I have to manually fix every build. just my 2 cents, would still love to hear if anyone has an elegant solution to this issue.
Super-late response, but for anyone else checking here: I had this exact same requirement, and someone else has made a plugin for it:
https://wordpress.org/plugins/acf-multiple-taxonomy/
This just adds another ACF field type called Multiple Taxonomies and works pretty much exactly how you’d expect.
@hube2,
Many thanks !!
This third solution seems to be very cool.
I need to do it as a “pre_get_posts” function with my $taxquery so as to hide some custom taxonomies.
I never did a loop inside a function inside my functions.php, the loop is inside my archives-films.php.
I have to test this, I will do it tomorrow and keep you in touch.
Thanks for all.
Best.
Pierre
There is a 3rd choice I just thought of.
You could do one query, loop over the posts once to show the ones that have a date and then loop over them a second time to show posts without a date. Given that you are showing all posts this might be the way I would go.
// posts with date value
While (have_posts()) {
the_post();
if (get_field('date_field') == '') {
// skip it
continue;
}
// show this post
}
rewind_posts();
// posts without date value
while (have_posts()) {
the_post();
if (get_field('date_field') != '') {
// end of posts without date, exit loop
break;
}
// show this post
}
Hi,
Please find the link.
I tried to add values to repeatedly created group multiple times with same key from frontend but that didnt worked.
It is not possible to short the posts in the way that you want to sort them. In MySQL empty strings are less than non-empty strings. When sorting in ASC order they will always come first. If you use number as the data type the value would be 0 (zero) again lower than any post with a date.
There are really 2 choices
1) ensure that every post has a value
2) do 2 queries, first get all posts with a value and then get all posts with empty values.
I don’t know what you mean by
multiple values for same field key
Thank you John,
Clearly this is the situation when one of us knows what he is talking about and the other one doesn’t. To be clear: you are the one that knows! đŸ™‚
I am afraid your reply left me none the wiser. Is this something that could be tackled through functions.php? I think you are suggesting, and this bit I get, is that the issue is around the input being split into words and then doing a LIKE query on each word, which is what WP apparently does. But that this is not what’s happening with ACF? How do I tackle it?
You will have to search the field the same way that WP does a search. WP splits the input into words does a LIKE query on each word.
It is a complex operation. See:
FILE: …/wp-includes/class-wp-query.php
LINE: ~1358
protected function parse_search( &$q ) {
...
“multisite” is not a standard WP_Query arg. I would suggest contacting the developer of the plugin(?) that adds this ability.
Example for JS:
(function( $ ) {
'use strict';
acf.add_filter('date_picker_args', function(args, el) {
args.yearRange = "-1000:+50";
return args;
});
})( jQuery );
It works great. FYI for future visitors. The JS must be added AFTER acf-input is added. And example could be:
wp_enqueue_script( get_stylesheet_directory_uri() . 'js/yourfile.js', array( 'jquery', 'acf-input' ), '1.0', false );
@hube2 yes it’s exact, the date field exist but is empty on some posts.
I have updated to :
$query->set( 'meta_query', array(
'relation' => 'AND',
'date_clause' => array(
'key' => 'date_de_sortie',
'value' => '',
'compare' => '!=',
)
)
);
Now it displays only posts with date field not empty.
Posts whith empty date field are not displayed.
Hi John, thanks for this. It looks like what I’m looking for. Question: Is this filter only available with the pro version? I have a license ready but it would be the only reason to use the pro version đŸ˜‰ Thanks Sascha
Me neither… I don’t understand.
Yes my code is running.
I tried running it with :
//$query->set( 'tax_query', $taxquery );
It doesn’t interfer.
The field ‘date_de_sortie’ is my ACF date picker field.
If I set ‘date_clause’ => ‘DESC’, it effectively sorts posts in the opposite direction which is logical, but always posts without any ‘date_de_sortie’ are in first.
Hope I’m clear in my explanations ;-).
Best.
Pierre
I don’t see any reason that is should not be working.
Are you sure that the if statement is correct and that your code is running?
Have you tried removing the tax query to see if that is interfering?
Does the field ‘date_de_sortie’ actually exist in the DB on the posts you are trying to sort?
Hello @hube2
Ok, oh yes I understand.
I tried with “date_clause” and unfortunately it’s still the same…
Here is the updated code :
add_action( 'pre_get_posts', 'films_query' );
function films_query( $query ) {
if ( !is_admin() && $query->is_main_query() && is_post_type_archive( 'films' ) ) {
$cat_hide = get_field('categorie_des_films_a_masquer', 'options');
$taxquery = array(
array(
'taxonomy' => 'categorie_films',
'field' => 'term_id',
'terms' => $cat_hide,
'operator' => 'NOT IN',
)
);
$query->set( 'posts_per_page', '-1' );
$query->set( 'post_status', 'publish' );
$query->set( 'tax_query', $taxquery );
$query->set( 'meta_query', array(
'relation' => 'AND',
'date_clause' => array(
'key' => 'date_de_sortie',
'compare' => 'EXISTS',
)
)
);
$query->set( 'orderby', array(
'date_clause' => 'ASC',
'title' => 'ASC',
)
);
}
}
No, I dont think so. I have no problem showing the posts I want to show using wp_query.
What I was is an effective ACF field that lets the customer login, select posts to be displayed and allows them to sort the order of the selected posts.
Once they have done this, I can get the ids and display them on the front end
Code you are using might help explain. I am unclear how you are showing the content of the gallery field to begin with. Gallery fields usually require some coding.
Using an ACF form in this case would not be the best way to accomplish what you want to do. ACF forms save data and then redirect back to the submitting page, or another page based on the form settings. There really isn’t any way to prevent this.
You want to create searching and filtering on an acf field. For this see the section titled
Dynamic $_GET parameters
on this page
https://www.advancedcustomfields.com/resources/query-posts-custom-fields/
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.