How to link custom field to shortcode? - wordpress

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.

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 - display posts in Elementor page using custom field where type is date

I've tried to implement this, while trying to apply some bits of code taken from here and there.
I'm using Elementor Page Builder in a website and I created a custom post type with ACF. Inside that post type, there's a field for a start_date.
The idea is to only show posts in homepage when the date is equal or higher to present date.
Right now the code is like this:
add_action( 'elementor/query/por_data', function( $query ) {
// Here we set the query to display posts
// after specific date
$query->set( 'date_query', array(
array(
'after' => 'May 17, 2020',
)
) );
} );
This obviously just shows posts created after 17 May 2020, which isn't what I want. The idea is to grab the date from that ACF field, compare it with current date and show results accordingly. The custom field type name is "curso"
Found this piece of code but can seem to merge the two together as I have very to little knowledge in programming (yet)
$args = array(
'post_type' => 'events',
'orderby' => 'event_date',
'meta_key' => 'event_date',
'order' => 'ASC',
'meta_query' => array(
array(
'key' => 'event_date',
'value' => date('Y-m-d',time()),
'compare' => '>'
)
),
);
$q = new WP_Query($args);
var_dump($q->request);
Can someone help? Thanks
I tried to explain it in the comments - hopefully you will get a better understanding of it now.
add_action( 'elementor/query/por_data', function( $query ) {
$meta_query = $query->get( 'meta_query' );
// Append our meta query instead of overwriting all elementors own metaqueries
if($meta_query == ""){
$meta_query = array();
}
$meta_query[] = array(
array(
'key' => 'start_date', //Or whatever you field is called
'value' => date('Y-m-d',time()), //Make sure that the format is correct here
'compare' => '>' //Means that the value should be higher than the key, so this is what you want - If you also need the current dates posts, then you need to use '>=' instead
)
);
$query->set( 'meta_query', $meta_query ); //since we appended our query, you can safely set the meta querie now.
} );
You might need to tweak it a little, but this will lead you the right way.

ACF Query Posts by Repeater Field Not Empty

This question seems to be unanswered on the internet, perhaps because it is not possible. I simply want to query all posts where the repeater field 'has rows'. i.e.
$args = array(
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'repeater_field',
'value' => '',
'compare' => '=!'
)
);
I know an alternative is to run if statements inside the loop to filter them out, but that messes with some other logic which is based off of the number of posts in the query.
Does anyone have any thoughts on this?
Let's consider you have a Repeater Field labeled My Repeater Field and this repeater contains at least one field labeled A Field on the Repeater.
Assuming you have the default wp_ table prefix, you need to look at the DB's wp_postmeta table to notice that values for this field on the repeater are stored with the meta_key:
NAME_OF_THE_REPEATER_index_NAME_OF_FIELD_ON_THE_REPEATER
So, in our case, if a post has 3 rows on the repeater field, its values will be stored as:
my_repeater_field_0_a_field_on_the_repeater
my_repeater_field_1_a_field_on_the_repeater
my_repeater_field_2_a_field_on_the_repeater
Knowing this, if you want to query all posts having at least ONE row on the repeater, you could do:
$meta_query = [
[
'key' => 'my_repeater_field_0_a_field_on_the_repeater',
'compare' => 'EXISTS',
]
];
$args = [
'posts_per_page' => -1,
'orderby' => 'date',
'order' => 'DESC',
'meta_query' => $meta_query,
'post_type' => 'post',
'post_status' => 'publish',
];
$posts_array = get_posts( $args );
Note: As stated on WP Docs, you can only use the EXISTS comparison on WP >= 3.5 and you don't need to specify a value when using the 'EXISTS' or 'NOT EXISTS' comparisons in WordPress 3.9 and up. I'm also assuming you are using PHP >= 5.4 so you can use short array syntax. If not, just replace [] for array().
You can query the wordpress database using the $wpdb object. ACF fields are saved in prod_postmeta on the database so that is where you will run your query. Your meta_value will be the key of your repeater field, so make sure you replace that in the query below. All keys for any ACF field with start out with field_ and then random characters/digits will follow like seen below. Then once you have the post id, you can run get_post() on those post ids. Let me know if you need anything else or have questions.
global $wpdb;
$results = $wpdb->get_results("SELECT post_id from prod_postmeta WHERE meta_value = 'field_534eeaaa74199'");
$echo $results;
This works. I have test it. Only by the "Welcome World" post it doesn't work.
$args = array(
'post_type'=> 'post',
'posts_per_page' => -1,
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'repeater_field',
'value' => '0',
'compare' => '!='
)
));
$the_query = new WP_Query( $args );
The "repeater_field" is the name of the field, not the field_key. Here is the count of the rows.

search with all field including custom field 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

Exclude post custom field wordpress

How to exclude a post that has empty custom field (for example "slides") from the wordpress loop?
You use meta_query in your Wp_query like this:
In this example they are only showing posts where the custom field "color" doesn't have the value "blue"
$args = array(
'post_type' => 'product',
'meta_query' => array(
array(
'key' => 'color',
'value' => 'blue',
'compare' => 'NOT LIKE'
)
)
);
$query = new WP_Query( $args );
See this page: http://codex.wordpress.org/Class_Reference/WP_Query and scroll to the "Custom Field Parameters" section.

Resources