Drupal 7 API + Taxonomy - php

I'm attempting to build a small tool with PHP to import content from my current CMS into Drupal 7 because I have about 10k+ articles to bring in. So far I've gotten the title, summary, body, author and published date to come through, but when it comes to categories (tags), I am completely baffled.
Each of my current categories/tags are stored in a database table, each having their own ID, name and description. I can pull this out per article and sort it however I'd like (string, array, etc).
During my import, I'm guessing I should do something like this:
$node->field_tags = array(
'und' => array(
array(
'Update',
'News',
'Report'
)
)
);
I've also tried:
$node->field_tags = array(
'Update',
'News',
'Report'
);
But these nor feeding in a comma separated string of words doesn't work. The Drupal 7 API documentation doesn't seem to explain this anywhere that I've found.
What's the format for sending tags through or what's the documentation page I haven't been able to locate? Thank you in advance!

Term fields in Drupal 7 are related to physical taxonomy terms, so you'll need to create a term for each category, and then add that reference as the field's value.
This code might help:
// Load the tags vocabulary
$vocab = taxonomy_vocabulary_machine_name_load('tags');
$term = new stdClass;
$term->vid = $vocab->vid; // Attach the vocab id to the new term
$term->name = 'Category Name'; // Give the term a name
taxonomy_term_save($term); // Save it
// Add the tags field
$node->field_tags = array(
LANGUAGE_NONE => array(
'tid' => $term->tid // Relate the field to the new category term using it's tid (term id)
)
);

Related

Show all posts under a same custom field

So, I have a custom field called "geo_location".
And under that field, there are total 5 different values (1 to 5).
Now, there are total 50 posts that have one of the five values.
For example, post#1 has geo_location="1" and post#2 has geo_location="3" etc.
Is there a way to show all the posts under the same 'geo_location' value?
So for example, I want to show all the posts under the same geo_location="1".
how can I achieve this?
Thank you.
You need to use WP_Query to create a database query using your custom meta-data.
This is done like this:
$args = array(
'meta_key' => 'geo_location',
'meta_value' => '1'
);
$query = new WP_Query( $args );
Then you can output the result using the loop in the usual way.

Display product isbn & mpn under each product on category page/tpl

I am using opencart (1.5.6.4) and was wondering how to I display additional product data under each product, specifically i want to display the product isbn and mpn.
I am unsure on how to make the product isbn & mpn accessible within the products loop of my category.tpl
For example I want to be able to use something along the following lines to display the data:
echo $product['mpn'];
echo $product['isbn'];
I am sure this has been asked many times or solved somewhere on the internet, though google couldn't provide me with the correct results right now...
Here's only a short how-to:
Edit the catalog/model/catalog/product.php model and search for method getProducts() - in the SQL provide your properties to be selected as well. Find
$sql = "SELECT p.product_id, ... AS special";
and change it to
$sql = "SELECT p.product_id, ... AS special, p.isbn, p.mpn";
supposing these two properties are saved in the product table.
Edit the catalog/controller/product/category.php controller and search for the loop where the product data is populated and processed until it is finally being assigned to a final array of products, looks like:
$this->data['products'][] = array(
'product_id' => $result['product_id'],
'thumb' => $image,
// ...
);
Here you need to add your new properties:
$this->data['products'][] = array(
'product_id' => $result['product_id'],
'thumb' => $image,
// ...,
'isbn' => $result['isbn'],
'mpn' => $result['mpn'],
);
Then in your template (category.tpl) you can easily display them in the foreach loop using the code in your question (make sure you adapt it for the correct HTML).

How to inject a virtual post (i.e. without adding it to the database)

I use a WordPress blog and I want to show a post without adding anything to database.
What I want to say is:
I generate a post when page loads,and prepend it in homepage.
I've searched and found wp_insert_post() function but it also add to database.
How can i do this with php?
For example:
There is a post array which is generated by a query.How can I insert my post to this array before page loaded?
I want clear my idea.Here's step by step what i want.
*1)*Im generating an array like that
$arr['title] = "my title",
$arr['content'] = "my content",
*2)*WP sends a query to database and have the posts am i right? And there is an array,to show on the theme and main page?
At this point i want to add my external array(generated in step1 ) to this array(generated by WP via a query)
3) By this way i will be able to add a post without adding it to my database.
You can simply add your virtual post in one of your theme templates as raw HTML.
Alternatively, if you're feeling adventurous, you could modify the main query results and include your post inside:
add_action('loop_start', function($query){
// create the post and fill up the fields
$post = new WP_Post((object)array(
'ID' => -1,
'post_title' => 'Bla blah',
'post_content' => 'Your content',
));
// add it to the internal cache, so WP doesn't fire a database query for it
// -1 is the ID of your post
if(!wp_cache_get(-1, 'posts'))
wp_cache_set(-1, $post, 'posts');
// prepend it to the query
array_unshift($query->posts, $post);
});
The currently accepted answer causes the new post to delete the last post in the loop, because it doesn't update the post count. Here's my modified version that also includes:
Support for empty categories.
Only one place to declare the new post's ID.
Adding is_main_query() as the person who originally answered mentioned in a comment.
A setting to decide if the new post should be appended or prepended.
Hiding the post's date because otherwise you get something like 00000000. I could have used a dynamic date but it may be bad SEO to keep updating the date without updating the content.
Hiding the post's comment link because it just leads to the homepage.
A setting to control the post type. You might prefer "page" because "post" displays a general category, which I found no way to bypass. "Page" also looks more distinguished among other posts, assuming that's a good thing.
Here's the modified code:
function virtual_post($query) {
$post_type = 'page'; // default is post
if (get_class($query)=='WP')
$query = $GLOBALS['wp_query'];
if ($query->is_main_query()) {
$append = true; // or prepend
// create the post and fill up the fields
$post = new WP_Post((object)array(
'ID' => -1,
'post_title' => 'Dummy post',
'post_content' => 'This is a fake virtual post.',
'post_date' => '',
'comment_status' => 'closed'
));
if ($post_type <> 'post')
$post->post_type = $post_type;
// add it to the internal cache, so WP doesn't fire a database query for it
if(!wp_cache_get($post->ID, 'posts')) {
wp_cache_set($post->ID, $post, 'posts');
if ($query->post_count==0 || $append)
$query->posts[] = $post;
else
array_unshift($query->posts, $post);
$query->post_count++;
}
}
}
$virtual_post_settings = array('enable' => true, 'include_empty_categories' => true);
if ($virtual_post_settings['enable']) {
if ($virtual_post_settings['include_empty_categories'])
add_action('wp', 'virtual_post');
else
add_action('loop_start', 'virtual_post');
}

Wordpress specific order of echo post meta checkboxes

I'm a php newbie and a first time poster.
I am working on a wordpress site where I need a discography.
I have successfully:
Created my custom post type : Albums
Added custom meta boxes and custom fields to admin post edit page
Made an archive page and echoed all the custom field meta.
http://squarerecording.com/albums/
One of my custom fields is a series of 5 checkboxes: the last line of each album on that archive page
array(
'name' => 'Services Rendered',
'desc' => 'field description (optional)',
'id' => $prefix . 'services',
'type' => 'multicheck',
'options' => array(
'R' => 'Recorded',
'Mi' => 'Mixed',
'Ma' => 'Mastered',
'P' => 'Produced',
'RMV' => 'Re-mastered for vinyl',
),
),
although I have successfully echoed the "Services Rendered" in a comma separated format, the order is different for each post (P,RMV,R,Mi,Ma-for the first one, P,Ma,Mi,R,RMV-for the second, etc.)
Here is the code for the archive page that outputs "Services Rendered":
<?php $key="sqr_services"; get_post_meta($post->ID, $key);
$sqr_services = get_post_meta( $post->ID, $key );
$comma_sep_services = implode(",", $sqr_services );
echo $comma_sep_services;
?>
My question is: What do I need to do so that they are listed in the same order that they appear on the edit page ( R,Mi,Ma,P,RMV)? Bearing in mind that they will not always ALL be checked.
I have tried messing around with unserializing but i don't know enough about it.
Any help or a point in the right direction would be greatly appreciated!
Thanks
This post might be of interest. It's one of the few solutions I've seen that doesn't involve editing Wordpress core files.
You could also try this modified get_post_custom function that orders the key/value pairs by meta id instead of the "date modified" order returned by get_post_custom.
A third option would be to sift through this answer.
Update: Silly Me! PHP can sort arrays any darn way you want, and it just gets a teeny bit tricky when that's not numerical or alphabetical. If alphabetization works for you, attempt the following:
$key="sqr_services";
get_post_meta($post->ID, $key);
$sqr_services = get_post_meta( $post->ID, $key );
asort($sqr_services);
foreach ($sqr_services as $key => $val) {
echo "$val\n";
}
If you want to learn yourself more complex array sorting, be my guest. Me? I'll stick to ABCs.
Update: Silly You!
In response to your comment about checking for and displaying individual values in your $sqr_services array, alls you needs to do is check in_array():
if (in_array('Recorded', $sqr_services)){echo 'Recorded';}
if (in_array('Mixed',$sqr_services)){echo 'Mixed';}
if (in_array('Mastered',$sqr_services)){echo 'Mastered';}
if (in_array('Produced',$sqr_services)){echo 'Produced';}
if (in_array('Re-mastered for vinyl',$sqr_services)){echo 'Re-mastered for vinyl';}
It's less flexible in that if you add a value to the array (add a new tax term), it won't start showing up until you check for it in your loop. I'm guessing that's not gonna be a problem, though, as this seems like a pretty finite set of options.

WordPress "wp_list_comments" custom order

I'm using WordPress 3.0.1 and want to order the comments from a post using a rating custom field.
Is this possible? I'm already using the callback property from wp_list_comments to customize the appearance of the comments.
Unfortunately this way I can only access the comments one by one and can't affect the order of the all result array.
I've already have a table with all the votes from the users.
Thanks in advance.
// get comments of post 1234
$comments = get_comments( array('post_id' => 1234) );
// ... order your comments collection using php (eg. usort) here ...
// print your comments
wp_list_comments( array( 'callback' => 'woocommerce_comments' ), $comments);
Try $comments = get_comments('postId=x');. It should be indexed by comment id. You can then look up the comment rating in your table.

Categories