I have a view that filters on a URL - e.g google.com
However, it doesn't match against the URL field unless I 're-save' the custom post manually.
Here is my workflow.
CRED form - submits the new Article post, whilst doing this it also creates a Website post (IF there is 'not' one matching the custom URL field - which is identical on both records) - This all works fine.
On the Article post, there is a View, this view looks up and matches against both custom field values and IF there is a match it creates a hyperlink to the Website custom post type.
IF on the VIEW I remove the filtering and set the Limit and Offset to more than 1, I can see that the view can find the Website CPT exists that has just been created... however, when I go to apply the filter it says it cannot be found.
IF I go into that Website post and 'Save' manually - then suddenly I can see this in the view?
Why is this behavior happening? As I have a very similar view on the Website page that matches all the Articles with the Website URL and it appears to be fine.
Attached are some images
Here is the CRED_SAVE_DATA
<?php
/**
* Check on EDIT or New post for a website match in the 'Website' CPT - if not found add one.
*/
toolset_snippet_security_check() or die( 'Direct access is not allowed' );
//FUNCTION TO CHECK IF Exsists
function meta_value_exists($your_meta_value) {
$args = array(
'post_type' => 'website',
'post_status' => 'publish',
'numberposts' => 1,
'meta_query' => array(
array('key' => 'wpcf-naked-website-domain',
'value' => $your_meta_value,
'compare' => 'REGEXP'
)),
);
$current_post = get_posts($args);
if( $current_post ) {
return true;
} else {
return false;
}
}
// CRED_SAVE_DATA
add_action( 'cred_save_data', 'url_save_check_quickadd_website', 10, 2 );
function url_save_check_quickadd_website( $post_id, $form_data ){
$forms = array( 1354 );
if ( in_array($form_data['id'], $forms) ){
if (isset($_POST['quickurl'])){
$url = strtolower($_POST['quickurl']);
$urlhost = parse_url($url);
$urlhostwww = preg_replace('/^www\./', '', $urlhost['host']);
$urldomain = preg_replace('/^(?:[-a-z0-9_]+\.)?([-a-z0-9_]+\..+)$/', '$1',$urlhost['host'])."\n";
//Start
// $field_id = $_POST['quickurl'];
$field_id = $urldomain;
if(meta_value_exists($field_id)){
// do something if exists
//update_post_meta($post_id, 'wpcf-article-teaser', 'All ready there');
} else {
// do something if not exists
// update_post_meta($post_id, 'wpcf-article-teaser', 'added a new one');
$new_post = array(
'post_title' => $urldomain,
'post_content' => '',
'post_status' => 'publish',
'post_date' => date('Y-m-d H:i:s'),
'post_author' => '',
'post_type' => 'website',
//THE META DATA Insert
'meta_input' => array('wpcf-naked-website-domain' => $urldomain ),
);
wp_insert_post($new_post);
}
//Finish
}
}
}