How to get meta value in wp_attachment_metadata - arrays

want to get wp_attachment_metadata in my own way. I want to get the file name :
a:5:{s:5:"width";i:500;s:6:"height";i:500;s:4:"file";s:25:"2016/08/sprite_1500ml.jpg";s:5:"sizes";a:5:{s:9:"thumbnail";a:4:{s:4:"file";s:25:"sprite_1500ml-150x150.jpg";s:5:"width";i:150;s:6:"height";i:150;s:9:"mime-type";s:10:"image/jpeg";}s:6:"medium";a:4:{s:4:"file";s:25:"sprite_1500ml-300x300.jpg";s:5:"width";i:300;s:6:"height";i:300;s:9:"mime-type";s:10:"image/jpeg";}s:14:"shop_thumbnail";a:4:{s:4:"file";s:25:"sprite_1500ml-180x180.jpg";s:5:"width";i:180;s:6:"height";i:180;s:9:"mime-type";s:10:"image/jpeg";}s:12:"shop_catalog";a:4:{s:4:"file";s:25:"sprite_1500ml-300x300.jpg";s:5:"width";i:300;s:6:"height";i:300;s:9:"mime-type";s:10:"image/jpeg";}s:11:"shop_single";a:4:{s:4:"file";s:25:"sprite_1500ml-400x400.jpg";s:5:"width";i:400;s:6:"height";i:400;s:9:"mime-type";s:10:"image/jpeg";}}s:10:"image_meta";a:12:{s:8:"aperture";s:1:"0";s:6:"credit";s:0:"";s:6:"camera";s:0:"";s:7:"caption";s:0:"";s:17:"created_timestamp";s:1:"0";s:9:"copyright";s:0:"";s:12:"focal_length";s:1:"0";s:3:"iso";s:1:"0";s:13:"shutter_speed";s:1:"0";s:5:"title";s:0:"";s:11:"orientation";s:1:"0";s:8:"keywords";a:0:{}}}
Well, I am stuck. I don’t know how to separate that array. Anyone knows how to separate that array in order I can get the file name (url file).
my code now :
$command = $_GET['command'];
switch ($command) {
case 'list_product':
$loop = new WP_Query(
array(
'post_type' => 'product'
)
);
if( $loop->have_posts() ) :
$data = array( "api_status" => 1, "api_message" => "success");
$meta = array();
while ( $loop->have_posts() ) : $loop->the_post();
$meta[] = array(
"id" => get_the_ID(),
"post_name" => get_the_title(),
"stock_status" => get_post_meta( get_the_ID(), '_stock_status', true ),
"price" => get_post_meta( get_the_ID(), '_price', true ),
"reguler_price" => get_post_meta( get_the_ID(), '_regular_price', true ),
"image" => get_post_meta( get_the__ID(), '_wp_attachment_metadata', true ),
);
endwhile;
endif;
echo json_encode($meta);
break;
i mean when i use :
"image" => get_post_meta( get_the__ID(), '_wp_attachment_metadata', true ),
my result doesnt show anything
what improvement in my code so its can work like what i want ?

If you want an image url, I show you another way to to this. I suppose you want the post thumbnail of a spicific post.
// First we get the image id
$post_thumbnail_id = get_post_thumbnail_id( get_the_ID() );
// Then we get the image data, we will get an array with some informations
$image = wp_get_attachment_image_src( $post_thumbnail_id, 'large' );
// the image url is the first index of this array
$image_url = $image[0];
In this example you get the image url for the largeversion. If you want antoher size, just change it to e.g. meidum or full. Read the documentation of wp_get_attachment_image_src() for more details.
This code must be placed inside the loop. Make sure that get_the_ID() returns the current POST ID. Make also sure that the POST have a post thumbnail.
Basically you can get an image url if you have an ID of an attachmend post (image).

Related

How can I get an array of custom categories in Wordpress?

I have registered a custom taxonomy as part of my custom post type, but when passing it through to get_categories() it returns an empty array. Any ideas as to why?
// Register FAQ Categories taxonomy
function bv_faq_register_categories() {
register_taxonomy(
'faq-category',
'faq',
array(
'label' => 'Categories',
'rewrite' => array('slug' => 'faq-category'),
'hierarchical' => true
)
);
}
add_action('init', 'bv_faq_register_categories');
// Category view
$categories = get_categories(array(
'taxonomy' => 'faq-category'
));
$categories is returning an empty array.
Have you tried get_terms instead?
$categories = get_terms( 'faq-category', array(
'orderby' => 'count',
'hide_empty' => 0
) );
Like #AD Styles said I would use get_terms using the custom taxonomy, to expand a bit here's some example code:
<?php
$post_type = 'faq-category';
// Get all the taxonomies for this post type
$taxonomies = get_object_taxonomies( (object) array( 'post_type' => $post_type ) );
foreach( $taxonomies as $taxonomy ) :
// Gets every "category" (term) in this taxonomy to get the respective posts
$terms = get_terms( array(
'taxonomy' => $taxonomy,
'parent' => 0
) );
foreach( $terms as $term ) :
echo "<h1>".$term->name."</h1>";
endforeach;
endforeach;
?>
Your code looks ok. Do you have assigned this category to any post/post type?
If not then you will get an empty result. for testing you can set 'hide_empty' = false like this:
// Category view
$categories = get_categories(array(
'taxonomy' => 'faq-category',
'hide_empty' => false // set it true
));
Also, you can use get_terms() function.

Merge multiple ACF variables in one array

I'm using ACF relationship fields. I'm displaying multiple hand selected posts blocks. There is a last posts block where I want to exclude all the hand selected ones before.
How do I make an array of all ACF's to select them to exclude them from the loop?
This is my code so far (not working, it works if I only use one variable)
<?php
$excluir = get_field('bloque_6_posts');
$excluir2 = get_field('bloque_2_posts');
$excluir3 = get_field('post_destacado');
$excluir4 = get_field('posts_destacados');
$excluir5 = get_field('bloque_4_posts');
$excluirtodo = array (
$excluir,
$excluir2,
$excluir3,
$excluir4,
$excluir5
);
$paged = (get_query_var('page')) ? get_query_var('page') : 1;
$args = array(
'posts_per_page' => 9,
'paged' => $paged,
'post__not_in' => $excluirtodo
);
$the_query = new WP_Query( $args );
?>
EDIT [SOLVED]: as #disinfor pointed on the comments the solution was array_merge instead of array
Adding my answer from the comments to help future visitors
You are currently passing an array of arrays to the post__not_in. You need to use array_merge to combine the arrays into a single array.
<?php
$excluir = get_field('bloque_6_posts');
$excluir2 = get_field('bloque_2_posts');
$excluir3 = get_field('post_destacado');
$excluir4 = get_field('posts_destacados');
$excluir5 = get_field('bloque_4_posts');
// NEW CODE HERE
$excluirtodo = array_merge(
$excluir,
$excluir2,
$excluir3,
$excluir4,
$excluir5
);
// END ARRAY_MERGE
$paged = (get_query_var('page')) ? get_query_var('page') : 1;
$args = array(
'posts_per_page' => 9,
'paged' => $paged,
'post__not_in' => $excluirtodo
);
$the_query = new WP_Query( $args );
?>
It seems to me a bad method to call 5 ACF fields and then combine them into an array at the time the page is built.
For me, this method is better:
1. Create a text AСF field - hide_excluir
2. Add filter in functions.php (When editing our page, all ACF fields with exceptions will be merged into an array and saved in the field that we created before).
add_filter('acf/save_post', 'excluir_post_filter', 20);
function excluir_post_filter($post_id) {
if ( $post_id != 2 ) //Change to your page ID (or if you need use post type or page template, need modify)
return;
$excluir = get_field('bloque_6_posts');
$excluir2 = get_field('bloque_2_posts');
$excluir3 = get_field('post_destacado');
$excluir4 = get_field('posts_destacados');
$excluir5 = get_field('bloque_4_posts');
$all_excluir = array_merge(
$excluir,
$excluir2,
$excluir3,
$excluir4,
$excluir5
);
update_post_meta($post_id, 'hide_excluir', $all_excluir ); //Save array to our field
}
3. We use our field with get_post_meta
$paged = (get_query_var('page')) ? get_query_var('page') : 1;
$args = array(
'posts_per_page' => 9,
'paged' => $paged,
'post__not_in' => get_post_meta( $post->ID, 'hide_excluir', true ) //Get our field with post array
);
$the_query = new WP_Query( $args );
For testing methods, you can install the Query Monitor plugin and see the difference in the number of queries to the database.

Display Image next to HABTM checkboxes

Cane Somebody give me an ideas on this Please!
I Generate multiple checkboxes from a table that is related with another table with HABTM relationship association.
I would like to generate multiple checkboxes with an image along with the text in the label.
My two tables are items and items_characteristics. So an Item HasAndBelongToMany characteristics, and an ItemCharacteristic HasAndBelongToMany Items.
echo $this->Form->input('Item.ItemCharacteristic',array(
'label' =>false,
'type'=>'select',
'multiple'=>'checkbox',
'options' => $itemCharacteristics ,
'selected' => $this->Html->value('ItemCharacteristic.ItemCharacteristic')
));
This code generate the list of the checkboxes properly and works perfect:
This is what i have:
Which is generated from DB from the table items_characteristics.
And this is what i wanna have:
Does Anyone have any Idea how i can achieve this Please?
I assume that in your controller you did something like:
$this->request->data = $this->Item->find('first', ... );
so that $data contains the information about selected characteristics in form of a subarray,
edit: I also assume that Item habtm ItemCharacteristic
then in your view
$checked_characteristics = Hash::extract($this->data, 'ItemCharacteristic.{n}.id');
foreach($itemCharacteristics as $id => $itemCharacteristic )
{
$checked = in_array($id, $checked_characteristics );
$img = $this->Html->image('cake.icon.png'); // put here the name
// of the icon you want to show
// based on the charateristic
// you are displayng
echo $this->Form->input(
'ItemCharacteristic.ItemCharacteristic.',
array(
'between' => $img,
'label' => $itemCharacteristic,
'value' => $id,
'type' => 'checkbox',
'checked' => $checked
)
);
}
edit: from your comment I understand that $itemCharacteristics come from a find('list') statement.
change it into a find('all', array('recursive' => -1));
now your code becomes
foreach($itemCharacteristics as $itemCharacteristic )
{
$id = $itemCharacteristic['ItemCharacteristic']['id'];
$icon_name = $itemCharacteristic['ItemCharacteristic']['icon_name']; //or wherever you get your icon path
$img = $this->Html->image($icon_name);
$itemCharacteristicName = $itemCharacteristic['ItemCharacteristic']['name'];
// same as above
}

No Response Through facebook api tag code

i am using this array for tag friends in a photo but no one is tagged , help me if i wrong.
$tags = array(
"tag_uid" => $id,
"tag_text" => $name,
"x" => 20,
"y" => 20
);
$facebook->api('/' . $new . '/tags?','post', array( $tags ));
this is because you are not doing the correct tags arrays. Try the following codes:
// Set tags limit
$f1 = $facebook->api('me/friends?limit=10');
// Get access token
$access_token = $facebook->getAccessToken();
// First post photo and then tag id
$post_photo = $facebook->api('/me/photos', 'POST', array(
'source' => '#' . $photo,
'message' => $message
)
);
// Creating Tags arrays for Tagging in the photo
foreach($f1['data'] as $fbu){
$tagx = array('tag_uid' => $fbu['id'],'x' => 30,'y' => 30 );
$ftags[] = $tagx;
}
// Tags generated now giving variable
$tagargs = array (
'tags' => json_encode($ftags),
'access_token' => $access_token,
);
// Posting the tags in photo
$result = $facebook->api('/' . $post_photo['id'] . '/tags', 'post', $tagargs);

get path of an uploaded file

This is my form to upload a photo
$form['Background_image'] = array(
'#type' => 'managed_file',
'#title' => t('Choose a background image'),
'#description' => t('Click "Browse..." to select an image to upload.'),
'#required' => TRUE,
'#upload_validators' => array('file_validate_extensions' => array('jpeg jpg png gif')),
'#upload_location' => 'public://backgroundimage/',
'#default_value' => $this->options['Background_image'],
);
I tried this function that get the current uploaded image and return its path, but still not working: what is missing:
function image_path()
{
$f = file_load($this->options['Background_image']);
//this too is not working:
//$f = file_load($form_state['values']['Background_image']);
$url_image = file_create_url($f->uri);
print_r($url_image);
return ($url_image);
}
Assuming your value $this->options['Background_image'] is the file id (a.k.a. fid) then the only issue I see with the second set of code is with your print_r statement. file_create_url returns a string, not an object or array, so you only have to print the output.
So replace:
print_r($url_image);
With:
print $url_image;
and it'll output the image's full url (including http) to the browser. Or simply remove the print_r statement if you are returning the value for use in another function.

Resources