Gah, I should have paid more attention to the commented out section, I didn’t even notice that you had tried my suggestion. Sorry about that.
Is there any chance you can provide more details on the server setups that have problems? I can try to recreate those setups and see if there’s a better way to detect which specific configurations require stripslashes_deep() and which do not. I’d be willing to invest some time to finding a solution.
I hesitate to turn magic quotes on on my server because it’s been deprecated in PHP 5.3 and removed in PHP 5.4 (the community has decided it’s a bad way to solve the sanitization problem). I also can’t see a solution where I can hook in after stripslashes_deep() has run in your code, because I have no way of knowing where slashes were removed from. My current solution is to hack your plugin and comment out that line, but (a) that leaves me in the lurch whenever you roll out updates, and (b) it still leaves others with the problem of data loss of backslashes in their ACF wysiwyg content (at least if they’re running newer versions of PHP).
I suppose a fall back solution is to expose a WordPress option that lets us end users choose whether to have that particular call to stripslashes_deep() enabled. That way you can leave it enabled by default to appease your legacy users, and I (and others) can disable it without hacking the plugin code.
Let me know which way you’d prefer to go, and I can either (a) research the problem setups with more info from you, or (b) submit a pull request with the code to expose a WordPress option (it would probably appear when editing a field definition that’s set to “Wysiwyg Editor,” under “Show Media Upload Buttons” and “Conditional Logic”).
Hey thanks Elliot, I’ve figured this out, however, now I am trying to figure out how to remove a nested repeater row. It seems simple enough using code like: do_action('acf/delete_value', 101, 'causes_1_cause_id' );
Doing this removes the field from the database, however, it does not update the count field in the database and it does not decrease each subsequent row number by one. As you can see, running the code above results in the table looking like the screenshot below:
causes
should have been changed to 2 and (_)causes_2_cause_id
should have been changed to (_)causes_1_cause_id
Is there a built-in function that will allow me to do this, or some direction on how to accomplish it?
Thanks again!
Hi!
First off, move wp_reset_postdata(); right under the endforeach; in your related posts loop.. that should fix the error with the incorrect ID.
Secondly.. there doesn’t seem to be a default archive link for posts if you don’t include a date or category tag (like http://www.demo.com/2012/). I honestly cant think of a smooth way to solve this the way it’s set up at the moment.. If I where you I’d either rethink it all or id create categories thats named the same as the companies but with a permalink of something like companyname-cat and assign the posts to them as well. That way you could just put this for the readmore link:
<?php
$cat = get_category_by_slug($post->slug.'-cat');
$catlink = get_category_link($cat->term_id);
?>
<a href="<?php echo $catlink; ?>">View all</a>
then you can check in archive.php if it’s trying to display a category with
if(is_cat()){
//the archive is displaying posts from a category
}
If this solution doesn’t work you could create a custom taxonomy and assign it to the posts (besides categories and tags) and use them instead and change the functions accordingly..
<?php
$cat = get_term_by('slug, '$post->slug.'-cat', 'your-taxonomy-slugname');
$catlink = get_category_link($cat->term_id);
?>
<a href="<?php echo $catlink; ?>">View all</a>
and:
if(is_tax()){
//the archive is displaying a term from a taxoonomy
}
Hi @Jonathan,
No it isn’t my theme, it’s just the placement of the “related posts” is on the sidebar which is out of the loop due to design issues 🙂
Hmmm, I manually typed in this URL (http://website.dev/?company=240) and it still gives me an error. Let me recap for you again, so perhaps you could point out to me what I’m doing wrong T_T
In my archives.php, here is the code:
<?php // if is a link to Read More from company single related post
if(isset($_GET['company'])){ ?>
<?php
$company_id = $_GET['company'];
// Show all related posts from the company singular page.
$items = get_posts(array(
'post_type' => 'post',
'meta_query' => array(
array(
'key' => 'company_relationship', // name of custom field
'value' => '"' . $company_id . '"', // matches exaclty "123", not just 123. This prevents a match for "1234"
'compare' => 'LIKE'
)
)
));
?>
<?php if( $items ): ?>
<?php endif; ?>
<?php } else { ?>
<?php while ( have_posts() ) : the_post(); // The Loop ?>
<?php get_template_part( 'content','excerpt' ); ?>
<?php endwhile; ?>
<?php } ?>
In my single-company.php, here is the code:
<?php
/*
* Query posts for a relationship value.
* This method uses the meta_query LIKE to match the string "123" to the database value a:1:{i:0;s:3:"123";} (serialized array)
*/
$posts = get_posts(array(
'post_type' => 'post',
'orderby' => 'ID',
'order' => 'DESC',
'posts_per_page'=> 1,
'meta_query' => array(
array(
'key' => 'company_relationship', // name of custom field
'value' => '"' . get_the_ID() . '"', // matches exactly "123", not just 123. This prevents a match for "1234"
'compare' => 'LIKE'
)
)
));
if( $posts ) { ?>
<div class="relatedposts">
<h5>Related Posts</h5>
<?php foreach( $posts as $post ): ?>
<?php setup_postdata($post); ?>
<?php get_template_part( 'content', 'excerpt' ); ?>
<?php endforeach; ?>
<?php
$company_ID = $post->ID; //get the id of the current company
$archive = get_post_type_archive_link('post').'?company='.$company_ID;
echo $archive;
?>
<a href="<?php echo $archive; ?>">View all</a>
</div><!-- div.relatedposts -->
<?php wp_reset_postdata(); ?>
<?php } ?>
I thank you in advance.
I think your theme is all wack.. getting loop issues and stuff all the time.
I don’t think thats the actual link but rather the a-tag is linked with just /?company=240 and since there’s nothing before that the link appears to go to that URL (which is the same as the one your on right?).
So that means get_post_type_archive_link isn’t working. What version of WP do you run? You’ll need to have it up to date (or atleast 3.1) for the function to work.
Hi @Jonathan,
Ah, when I echo out $archive
, I get ?company=862
. Yet the Company’s Post ID is 240.
I believe this is the same out-of-the-loop issue you also helped me with awhile back here!
However, even when I manually changed the link to 240, the URL is still:
http://website.dev/en/companies/COMPANY_NAME/?company=240
Which still gives me the error. Is this URL format correct?
ahaaa okay.
Well if you try to echo out the second row what do you get?
Hi Jonathan,
I believe the code you provided above is the code to be put in archives.php right? I can’t get it to work unless the “View all” link from the company-single.php works, which doesnt:
<?php
$company_ID = $post->ID; //get the id of the current company
$archive = get_post_type_archive_link('post').'?company='.$company_ID;
?>
Hi Elliot, thanks for your answer. Turns out I got it completelky wrong. I was using a normal checkbox without realizing there’s a true/false checkbox that des the job just fine. However, I’m still in a bit of trouble. I have this set up:
if(function_exists("register_field_group"))
{
register_field_group(array (
'id' => 'acf_featured-post',
'title' => 'Featured Post',
'fields' => array (
array (
'key' => 'field_5233c482612a9',
'label' => 'Feature Post?',
'name' => 'featured',
'type' => 'true_false',
'message' => 'Is this a featured post?',
'default_value' => 0,
),
),
'location' => array (
array (
array (
'param' => 'post_type',
'operator' => '==',
'value' => 'post',
'order_no' => 0,
'group_no' => 0,
),
),
),
'options' => array (
'position' => 'side',
'layout' => 'default',
'hide_on_screen' => array (
),
),
'menu_order' => 0,
));
}
And I have no truble calling the featured posts like this:
$posts = get_posts(array(
'meta_query' => array(
array(
'key' => 'featured',
'value' => '1',
'compare' => '=='
)
)
));
echo '<ul>';
if( $posts )
{
foreach( $posts as $post )
{
setup_postdata( $post );
echo '<li>'; the_post_thumbnail('slider'); the_title(); echo '</li>';
}
}
?>
</ul>
<?php wp_reset_postdata(); ?>
However, when I use another loop like this:
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<h2><a href="<?php the_permalink() ;?>"><?php the_title() ;?></a></h2>
<?php the_post_thumbnail('home'); ?>
<?php the_excerpt(); ?>
<?php endwhile; else: ?>
<p>Sorry, no posts to list</p>
<?php endif; ?>
<?php
global $wp_query;
$big = 999999999; // need an unlikely integer
echo paginate_links( array(
'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
'format' => '?paged=%#%',
'current' => max( 1, get_query_var('paged') ),
'total' => $wp_query->max_num_pages
) );
?>
I’m still getting the featured posts. I don’t quite get it, shouldn’t I be getting the rest of the posts?
Thanks a lot!
Hello again Nataliette.
With my previous solution, did you create a loop inside the if-statement for when you have company posts?
<?php get_header(); ?>
<?php // if is a link to Read More from company single related post
if(isset($_GET['company'])){ ?>
<?php
$company_id = $_GET['company'];
Show all related posts from the company's singular page.
$doctors = get_posts(array(
'post_type' => 'doctor',
'meta_query' => array(
array(
'key' => 'location', // name of custom field
'value' => '"' . $company_id . '"', // matches exaclty "123", not just 123. This prevents a match for "1234"
'compare' => 'LIKE'
)
)
));
?>
<?php if( $doctors ): ?>
//ETC. This is not a full loop
?>
<?php } else { ?>
<?php while ( have_posts() ) : the_post(); // The Loop ?>
<?php get_template_part( 'content','excerpt' ); ?>
<?php endwhile; ?>
<?php } ?>
<?php get_footer(); ?>
Hi @elliot and thanks for the quick reply.
Does this mean that it is impossible to order by a true/false ACF field at all, when using WP_Query ?
When the checkbox for that field is unchecked, does ACF save 0 or a NULL into the database ?
Is there a way to define a default value so there are no NULLs in database ?
I’m quite surprised because when you run a query against a table and order by a field, even records with NULL are ordered as expected..
Hi, could anyone help me with this?
I basically need a way to dynamically generate an archive of all normal posts that have a “relationship” with the Company-post-type.
Thank you in advance.
Hi @istrasoft
I think unfortunately, this is a limitation of the WP_Query object. For WP to order the posts, each post must have a value in the meta field ‘complete’. If no value is found, the SQL won’t run correctly for that post.
Perhaps there are some resources on Google that show how to use a default meta value for posts that don’t have a value?
Thanks
E
Hi @gleenk, lucky you I have a lazy Sunday and therefore I had some time to check out your code.
First of all, please change the “Return Value” on your “Relationship” field from “Post Objects” to “Post IDs”, this way we get a nice array of all the WP_Post->IDs instead of the full WP_Post objects. See the screenshot attached for more info.
So this brings me to the code:
<?php if ( get_field( 'schede_prodotti' ) ): ?>
<table class="product-list simple-list">
<?php while( has_sub_field( 'schede_prodotti' ) ): ?>
<tr>
<td class="title">
<?php
/**
* Get the IDs
*
* Returns an array of the IDs of the posts
* selected using the Relationship field.
*
* @see Screenshot: Change return value from Post Objects to Post IDs
* @var array
*/
$ids = get_sub_field( 'relazione_prodotti' );
/**
* Prepare the get_posts arguments
*
* With the 'orderby' => 'ttile' we accomplish the goal of having the
* products ordered by name instead of drag and drop.
*
* @see http://codex.wordpress.org/Template_Tags/get_posts#Parameters
* @var array
*/
$args = array(
'post__in' => $ids, // Use the ids from above
'order' => 'ASC', // Order
'orderby' => 'title' // Field to order the posts by
);
/**
* Query the Posts
*
* @var array
*/
$posts = get_posts( $args );
if ( $posts ) :
foreach ( $posts as $post ) : setup_postdata( $post ); ?>
<span><?php the_title(); ?></span><?php
endforeach;
wp_reset_postdata();
endif; // ( $posts )
?>
</td>
<td>
<a href="<?php the_sub_field('scheda_tecnica')?>" class="notifiche" data-tipo="Scheda Tecnica">Scarica la Scheda Tecnica</a>
</td>
<td>
<a href="<?php the_sub_field('scheda_di_sicurezza')?>" class="notifiche" data-tipo="Scheda di Sicurezza">Scarica la Scheda di Sicurezza</a>
</td>
</tr>
<?php endwhile; // has_sub_field( 'schede_prodotti' ) ?>
</table>
<?php endif; // get_field( 'schede_prodotti' ) ?>
For better readability, I uploaded the code as a Gist.
Please let me know if this solves your problem.
Ok I created a new code:
// add the __() functions for theme translations (by BaLu.LT)
$ignore_keys = array(
'id',
'key',
'name',
'type',
'field',
'operator',
'value',
'allorany',
'formatting',
'position',
'layout',
'save_format',
'preview_size',
'library',
'param',
'toolbar',
'media_upload',
'date_format',
'display_format',
'return_format',
'taxonomy',
'field_type',
'default_value'
);
$ikeys = "";
foreach ( $ignore_keys as &$ikey ) $ikeys .= "(?<!\'" . $ikey . ")";
$regular_expression = "/((?<=" . $ikeys . "\' => )\'.+\'(?=,))/";
$html = preg_replace( $regular_expression, "__($1, 'acf_export')", $html );
Few things to mention:
No translation support for ‘default_value’ using it as default text (alternative: placeholder)
No translation support for choices with identically matched keys to any of $ignore_keys or if it’s an integer
Hi Floris,
i had the same problem 2 days ago with a german language website and i`ve found a simple solution.
please see here: http://oenj.justbemotion.com/stockschiessen/ergebnisse-stockschiessen/ (first column, Mrz stands for März)
here is the solution i have used:
<?php
$dateStart = date_i18n("d M Y", strtotime(get_sub_field('stcksch_termine_datum1')));
?>
<?php if(get_sub_field('stcksch_termine_datum1')): ?>
<?php echo $dateStart; ?>
<?php endif; ?>
hope it helps.
regards,
andi
EDIT: i have used it as a subfield in a repeater field, so please use get_field instead of get_sub_field.
Weee! I was about to crate post for this topic and now I fount it on To-do.. sweeet!
Code that I’m using right now:
// add the __() function to specific strings for translation in theme
$html = preg_replace("/'title'(.*?)('.+?',)/", "'title'$1__($2 'acf_export'),", $html);
$html = preg_replace("/'label'(.*?)('.+?',)/", "'label'$1__($2 'acf_export'),", $html);
$html = preg_replace("/'instructions'(.*?)('.+?',)/", "'instructions'$1__($2 'acf_export'),", $html);
$html = preg_replace("/'button_label'(.*?)('.+?',)/", "'button_label'$1__($2 'acf_export'),", $html);
$html = preg_replace("/'placeholder'(.*?)('.+?',)/", "'placeholder'$1__($2 'acf_export'),", $html);
$html = preg_replace("/'message'(.*?)('.+?',)/", "'message'$1__($2 'acf_export'),", $html);
$html = preg_replace("/'prepend'(.*?)('.+?',)/", "'prepend'$1__($2 'acf_export'),", $html);
$html = preg_replace("/'append'(.*?)('.+?',)/", "'append'$1__($2 'acf_export'),", $html);
The ‘acf_export’ helps out to find and replace it with theme text domain.
This helps out, but could make some improvements and find a way to add translation functions to choices.
Hi Elliot,
I think I found the culprit. In Firefox I get the following:
{"wp-auth-check":true,"wp-refresh-post-lock":{"new_lock":"1379162061:11"},"wp-refresh-post-nonces":{"check":1},"server_time":1379162061}
So, I disabled all plugins and changed the theme to Twenty Twelve. Still same issue. So I did a Google Search on the message and it led me back to your forum, here .
Then, I went and looked at my Location Rules.
When I changed the second rule to a different Logged In User Type so that it didn’t apply to the user anymore, then the sorting of images in gallery field work. Once I set it back to the logged in user type for that post I get the above message in Firefox console.
Does that help?
Hi @gleenk it looks like you’ll have to retrieve the WP_Post IDs first and next perform an WP_Query or get_posts to order this stuff properly.
Have a look at the following post “Order Post Objects by menu_order”. The Post Object and the Relationship field both return an array of WP_Post objects, so this solution should be possible for you to use.
In your $args you’ll use the following to order by title 'orderby' => 'title'
Please let me know if this works for you…
Hi @squrler
Yes. Please read this documentation article:
http://www.advancedcustomfields.com/resources/how-to/how-to-get-values-from-another-page/
Hi @aaronrobb
Yes, this is completely possible. The question is, are you sure that you want to store this kind of information on each post. By doing so, if the user was to update their address, you would have incorrect data all over the place.
It may be smarter to use a user_field to relate the user to the post, then in the template load the address data from the related user.
Is this a possibility?
Hi @digisavvy
So just to clarify, changing the page template correctly triggers the AJA function to load in the new field group, however, it doesn’t hide the origional field group?
A few questions.
Can you post some screenshots of your fiedl groups showing the location rules?
Are you registering these field groups via PHP?
Can you check your console log for any JS errors during hte page load / AJA call?
Thanks
E
Hi @alexgold05
I can see at the top of your code that if the $post_id != ‘new_listing’, you return the $post_id.
This return
will stop any further code from working!
Perhaps you need to change your code to look more like this:
<?php
global $current_user;
// check if this is to be a new post
if( $post_id == 'new_listing' )
{
// Create a new post
$post = array(
'post_status' => get_field('default_listing_submission_status','option') ,
'post_title' => $_POST["fields"]['field_522f30035c85b'],
'post_content' => $_POST["fields"]['field_5230848453094'],
'post_type' => 'listing' ,
);
$post_id = wp_insert_post( $post );
}
return $post_id;
?>
I have taken the liberty of removing the update_post functionality as I don’t see how it does anything useful for the above code.
Thanks
E
Currently, there is no filter for the wp_list_categories
args within the taxonomy field.
You can edit the core/fields/taxonomy field to add on in, however this may be removed after updating.
I’ll add this to the to-do and add it into the core.
Cheers
E
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.