I have a website using WooCommerce in the Slovenian language that is being translated with WPML into multiple languages and English will become the default one.
Products were all set in Slovenian language and use a custom field (ACF) for an image. The products are translated but the images are duplicated which causes issues in one case. The image should be of the original product.
I have now fixed this by using this:
$preview_img_id = get_field( 'preview_image', apply_filters( 'wpml_object_id', get_the_ID(), 'product', FALSE, 'sl' ) );
But this is not a good solution in the long term because new products might be added in another language first.
What function can I use to get the ID of the original post/product?
I'm not sure I understand what "causes issues in one case", but I believe you can omit the $ulanguage_code and set $return_original_if_missing to TRUE. Documentation:
$ulanguage_code - (mixed) (Optional) If missing or NULL, it will use
the current language. If set to a language code, it will return a
translation for that language code or the original if the translation
is missing and $return_original_if_missing is set to TRUE. Default is
NULL
If you always want to get the ID of the original product, then maybe you could trick it into doing this by specifying a non-existent language code, for example:
apply_filters( 'wpml_object_id', get_the_ID(), 'product', TRUE, 'xx' )
Related
This has been driving me nuts trying to figure out: In my plugin I have a section that pulls all entries out of the Database - we'll say all posts, but I also display Pages and Categories - using this line of code:
$args_posts = array(
'numberposts' => -1,
'suppress_filters' => true
);
$posts = get_pages($args_posts);
foreach ( $pages as $page ) {
//formatting & display
}
...and then this get looped, formatted, and echoed using a foreach() statement.
The problem I'm having is that it runs fine on my server but when I have a friend try it it only shows 5 posts. The environment it needs to work under will end up having several thousand entries. Originally, the problem was there for me too but I didn't supply the arguments for get_posts() - I just set it as a variable itself using $posts = $get_pages(), and once I added the arguments it resolved. But my friend is still seeing only 5 entries displayed, even though there's no browser cache or server cache and other changes in the script are coming through.
Thank you in advance!
According to the Wordpress Docs:
Number
(integer) Sets the number of Pages to list. This causes the SQL LIMIT value to be defined. Default to no LIMIT. This parameter was
added with Version 2.8. Note: get_posts() uses the parameter
'numberposts' instead of 'number'. Second Note: it doesn't work if
used together with 'child_of'. Instead use 'parent' and set
'hierarchical' to false.
Link:
Documentation
In otherwords change numberposts to number
i am trying to translate my plugin options through WPML but it is not working.
Here is how i have placed my string in the plugin file
get_option(_e('my_label','my-text-domain'));
I have already scan my plugin through the WPML and have done translation in "German" while default is English.
Can anyone help me.
Thanks
The issue is that _e outputs the translated text.
You want __ (double underscore), which will return the translated text.
I would explore other solutions to this problem. In general you shouldn't be using translation to determine what option to pull from the database. The functionality of your plugin should not hinge on whether or not the translated text is a valid option name.
An alternative approach may be to use get_locale to fetch the current locale and then use that to determine the option name:
$option_name = get_locale() . 'my_label';
$label = get_option( $option_name );
Now you can still get the localized version of your option, without depending on translators to input the correct option name.
If you have to use the translation approach, I would use _x to explain that the translated text should be an option key.
get_option( _x('my_label', 'Must be a valid option name', 'my-text-domain') );
Provided that you have multiple entries in your meta table per language, try the double underscore function?:
get_option( __( 'my_label','my-text-domain' ) );
I'm using the ACF plugin for WordPress. I'm posting to a custom post type programmatically via my theme. Now, I thought I would be able to save an array of items to a custom field quite easily, but whenever I try to, nothing gets input. I simply get this error when I check the post screen in the admin area:
Warning: htmlspecialchars() expects parameter 1 to be string, array given in /home/lukeseag/public_html/spidr-wp/wp-content/plugins/advanced-custom-fields/core/fields/text.php on line 127
Now, I'm kind of throwing this out there for suggestions as to how I can go about solving this problem.
To give some context, I'm programmatically saving data input by a user, and creating a post with it. Much of the data is simple strings and numbers that can each have their own custom field.
But I have an unknown number of text strings and URL's (with an id number for each) coming through this page too, that need to be linked with the same post. I need to be able to output each set of text string and URL into their own div on the single.php post page. Now, ideally these text/url pairs would be saved in a single array so I can just loop through the array and output the data, but it looks like ACF doesn't want to let this happen?
Any suggestions as to how I can save this data and then output it easily on the single.php page? Each set of results will have an ID (number), text string and url.
Thanks!
This is exactly why all those "frameworks" are usually more pain than gain. they are designed to look flexible to the lazy but then they always prove useless on a real case scenario that is a bit more complex than anything that would actually be easily achieved without them.
Anyhow, as for your question.
The problem is that you are passing array instead of a string .
I am not going to go into debugging the plugin, and anyhow , more code and info is needed , so i will just give you ONE possible and easy solution, and that is to compose a pseudo-array string like so :
'ID|URL|STRING'
note that the delimiter pipe (|) is just an example, you could use ( , ) ( : ) ( # ) or whatever.
in other words :
$url = 'http://myurl.url/something';
$id = 35;
$string = 'my string';
$pseudo_r = $id . '|' . $url . '|' . $string . '|'; // results in 'ID|URL|STRING';
or you can use the implode() function
$pseudo_r = array(
"url" => $url,
"id" => $id ,
"string" => $string
);
$pseudo_r = implode("|", $pseudo_r); // gives a string
and pass it as a string that later on you can decompose it with explode() like so :
$pseudo_r = explode( '|', $pseudo_r ); // gives back the original array
( Please note that now the array is NON ASSOCIATIVE anymore . )
now, since you mentioned that each one of those is a set, you can compose the same pseudo array from the sets, using a different delimiter.
This might work for you , but essentially it is wrong .
why is it wrong ?
Because this is a generic and somewhat hackish solution that actually ignores your data type .
You are mixing 3 types of data types , URL , ID and a STRING .
All 3 ( should ) have potentially different validation schemes , and I suspect that the htmlspecialchars() part belong to the URL.
Another solution is just to find the right data field in the ACF settings or use a different field for each and then just DISPLAY it together , also using the implode() and explode() functions.
Yo can save the array using json_encode($your_array) and json_decode() to recover it. This works also for associative arrays.
Hard to define the Title of this Question....
I want to create a nice readable permalink structure for my 2 custom post types (CPT).
My first CPT "family" has the following rewrite-slug "family/%postname%" (all works fine)
The second CPT "childs" has a metabox where I can select the parent_id-field by choosing a CPT "family" where the child-CPT belongs to. That also works great.
Now I set the rewrite-slug for "childs" to "%parent_post_url%/child/%postname%" so that I can get the following URL "family/the-griffons/child/peter" . But when I call this URL wordpress displays a not-found-page. The crazy thing is that if I set the rewrite-slug hard to "family/the-griffons/child/%postname%" I can call the URL (both URLs are the same!!!)
So why toes WP throws an error when I try to get the URL dynamically but not when I hardcode the URL??
The child-parent relationship you think you have is not quite there. Based on what you've told us so far, it seems that all you have is a custom field denoting the "pseudo-parent" id. So what your question should really read is
How do I rewrite the first part of the cpt url, based on the cpt custom field value ?
because, as far as wordpress is concerned in your case, that's all that "parent id" really is- a custom field value.
you can try following the last part(Part 3.) of this tutorial, keeping in mind, that you'll want the actual path of the url of the "parent id" and not the cf "parent id" value itself, you'll have to implement something along the lines of:
$parent_id = get_post_meta($post_id, "your_cf_key_for_parent_id", true);
$full_parent_post_url = parse_url( get_permalink( $parent_id ) );
$parent_post_url = $full_parent_post_url['path'];
if(!$parent_post_url) { $parent_post_url = "default-fallback" }
$permalink = str_replace(‘%parent_post_url%’, $parent_post_url, $permalink);
return $permalink;
another relevant stackexchange answer:
using-custom-fields-in-custom-post-type-url
BUT as #c0ns0l3 mentioned using custom taxonomies IS the proper way to go about this.
I am creating a blog using only Wordpress's backend. I have found functions to get latest posts (wp_get_recent_posts) and all the required data I need. I do this by including wp-load so I have access to WP's functions.
However I cannot find anything that allows me to perform a search outside of Wordpress's theming loops as I have for the rest of the data.
I was hoping there was a search function where I can pass it a search query that could be in title, body content or tag name.
Am I missing something blindingly obvious in the documentation, there seems to be a function for everything else I need outside of WP's "loop".
Does that work for you?
// query for a given term ("search_term")
$search_query = new WP_Query();
$search_posts = $search_query->query('s=search_term');
Source
Answered by sanchothefat:
You can use get_posts() with a search parameter:
$results = get_posts( array( 's' => 'search term' ) );
https://wordpress.stackexchange.com/questions/74763/search-outside-of-the-loop/74766#74766