search for custom post on AJAX response in WORDPRESS
search for custom post on AJAX response in WORDPRESS
I search for a post with certain $value
in posts of custom post type with custom field in AJAX response. Custom field is provided by ACF plugin. When button is clicked then AJAX request is send and posts are searched through. If custom_field
of a post equals $value
then I get id
of that post if $value
doesn't exist in any of custom posts then post is created and I get id
of newly created post. Every post of custom_post
contains custom_field
. When I refresh page it works and post is not created for second time after second click. Otherwise the another click doesn't find a post so it is created again. I don't know why refresh helps.
$value
custom_field
$value
id
$value
id
custom_post
custom_field
In content-page.php
I call a function from my functions.php
that generates links with:
content-page.php
functions.php
jQuery(document).ready(function($) {
jQuery( "body" ).click(function( event ) {
var clicked = jQuery(event.target).prop("tagName");
if (clicked == "BUTTON"){
...
$.ajax({
... // here are created links
I send AJAX request when link got clicked with:
$.ajax({ // this is response for clicked link
url: example_ajax_obj.ajaxurl,
data: {
...
},
success:
...
It works - the request is processed and I got response data back. I use following function defined in functions.php
for a search:
functions.php
function searchForPost($value){
$args=array(
'post_type' => 'custom_post',
'meta_query' => array (
array (
'key' => 'custom_field',
'value' => $value,
)
)
);
$id = 0; //I use 0 later on as indicator of 'post not found'
$post = new WP_Query( $args );
if ( $post->have_posts() ) {
while ( $post->have_posts() ) {
$post->the_post();
$id = $post->post->ID ;
}
wp_reset_postdata();
}
return $id;
}
If post with certain value doesn't exist then it is created - it works (as 0 is default value).
When I click link another time, search doesn't find a post so it is created again.
AJAX response function is also in functions.php
.
functions.php
I want to search for
$value
in all posts of custom_type
. Every post of custom_type
contains custom_field
field. In the end I don't want $value
duplicates.– otakon
6 mins ago
$value
custom_type
custom_type
custom_field
$value
give me an example what is the actual
value
you are searching for, what is the name of the custom_post_type
and what is the name of the custom_field
? Also so I am clear you only one want to return one result.– Orlando P.
1 min ago
value
custom_post_type
custom_field
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
Are you looking for a collection of posts or just one? Your code and some of your language makes it seem like you only want one post. I would recommend removing the ajax aspect temporarily and do some var_dumps to see what posts you do find and what value you search for
– Orlando P.
18 mins ago