I try to add new node with unpublished.
I set the status = 0;
but when check this node the status = 1;
this code.
$node = new StdClass();
$node->type = 'article'; //giving it type
$node->title = $article->headline; //gives title
$node->body = $article->body; //gives body
$node->field_abstract[0]['value'] = $article->summary;
$node->field_is_syndigate[0]['value'] = 1;
$node->field_syndigate_first_time[0]['value'] = 1;
$node->language = $language->language; //'en' or 'ar'
$node->comment = 2; //read&write comment
//$node->created = $article->parsed_at;
$node->created = '';
$node->changed = $node->created;
$node->status = 0;
$node->promote = 0;
$node->sticky = 0;
$node->format = 4; // Editor Filtered HTML
$node->uid = $user->uid;
//$node->field_attribution[0]['value'] = $article->copyright; // attribution
$attribution = '';
if($article->logo_path){
$attribution = '<img src="http://pub.syndigate.info/logos/small/'.$article->title_id.'.png" title="'.$article->title_name.'" border="0" />';
if($article->website){
$attribution = ''.$attribution.'';
}
$attribution .= ' ';
}
$attribution .= $article->copyright;
$node->field_attribution[0]['value'] = $attribution; // attribution
$node->field_source[0]['value'] = $article->title_id; // source
node_save($node);
How can save the node as unpublished.
When creating a node programmatically, you need to set
$node->is_new = TRUE;
And if you are setting the $node->created to an empty value, I would suggest to get rid of this line from your script, node_save() will take care of it. Same for $node->changed as you give the same value, just delete this line from your script as well
hope this help
Related
I need to get current user comment details which is commented by him.
please try below code
global $user;
$query = db_select('comments', 'cm');
$query->fields(cm);
$query->condition('cm.uid', $user->uid, '=');
$result = $query->execute();
while ($chunk = $result->fetchObject()) {
print $chunk->subject; // comment title
print $chunk->nid; // on which node user enter comment
print $chunk->mail; // email of user
}
OR you can use view to create listing without using manual query.
goto structure->views->create new view and select comments option in Show Field setting.
Thanks,
Ankush
You Can create a view (view page or view block) of comments and add the RELATIONSHIPS with user (Comment: Author) and add the CONTEXTUAL FILTERS for user (author User: Uid)
$view = new view();
$view->name = 'comments';
$view->description = '';
$view->tag = 'default';
$view->base_table = 'comment';
$view->human_name = 'Comments';
$view->core = 7;
$view->api_version = '3.0';
$view->disabled = FALSE; /* Edit this to true to make a default view disabled initially */
/* Display: Master */
$handler = $view->new_display('default', 'Master', 'default');
$handler->display->display_options['title'] = 'Comments';
$handler->display->display_options['use_more_always'] = FALSE;
$handler->display->display_options['access']['type'] = 'perm';
$handler->display->display_options['access']['perm'] = 'access comments';
$handler->display->display_options['cache']['type'] = 'none';
$handler->display->display_options['query']['type'] = 'views_query';
$handler->display->display_options['exposed_form']['type'] = 'basic';
$handler->display->display_options['pager']['type'] = 'full';
$handler->display->display_options['pager']['options']['items_per_page'] = '10';
$handler->display->display_options['style_plugin'] = 'default';
$handler->display->display_options['row_plugin'] = 'comment';
/* Relationship: Comment: Content */
$handler->display->display_options['relationships']['nid']['id'] = 'nid';
$handler->display->display_options['relationships']['nid']['table'] = 'comment';
$handler->display->display_options['relationships']['nid']['field'] = 'nid';
$handler->display->display_options['relationships']['nid']['required'] = TRUE;
/* Relationship: Comment: Author */
$handler->display->display_options['relationships']['uid']['id'] = 'uid';
$handler->display->display_options['relationships']['uid']['table'] = 'comment';
$handler->display->display_options['relationships']['uid']['field'] = 'uid';
/* Field: Comment: Title */
$handler->display->display_options['fields']['subject']['id'] = 'subject';
$handler->display->display_options['fields']['subject']['table'] = 'comment';
$handler->display->display_options['fields']['subject']['field'] = 'subject';
$handler->display->display_options['fields']['subject']['label'] = '';
$handler->display->display_options['fields']['subject']['alter']['word_boundary'] = FALSE;
$handler->display->display_options['fields']['subject']['alter']['ellipsis'] = FALSE;
/* Sort criterion: Comment: Post date */
$handler->display->display_options['sorts']['created']['id'] = 'created';
$handler->display->display_options['sorts']['created']['table'] = 'comment';
$handler->display->display_options['sorts']['created']['field'] = 'created';
$handler->display->display_options['sorts']['created']['order'] = 'DESC';
/* Contextual filter: User: Uid */
$handler->display->display_options['arguments']['uid']['id'] = 'uid';
$handler->display->display_options['arguments']['uid']['table'] = 'users';
$handler->display->display_options['arguments']['uid']['field'] = 'uid';
$handler->display->display_options['arguments']['uid']['relationship'] = 'uid';
$handler->display->display_options['arguments']['uid']['default_argument_type'] = 'fixed';
$handler->display->display_options['arguments']['uid']['summary']['number_of_records'] = '0';
$handler->display->display_options['arguments']['uid']['summary']['format'] = 'default_summary';
$handler->display->display_options['arguments']['uid']['summary_options']['items_per_page'] = '25';
/* Filter criterion: Comment: Approved */
$handler->display->display_options['filters']['status']['id'] = 'status';
$handler->display->display_options['filters']['status']['table'] = 'comment';
$handler->display->display_options['filters']['status']['field'] = 'status';
$handler->display->display_options['filters']['status']['value'] = 1;
$handler->display->display_options['filters']['status']['group'] = 1;
$handler->display->display_options['filters']['status']['expose']['operator'] = FALSE;
/* Filter criterion: Content: Published */
$handler->display->display_options['filters']['status_node']['id'] = 'status_node';
$handler->display->display_options['filters']['status_node']['table'] = 'node';
$handler->display->display_options['filters']['status_node']['field'] = 'status';
$handler->display->display_options['filters']['status_node']['relationship'] = 'nid';
$handler->display->display_options['filters']['status_node']['value'] = 1;
$handler->display->display_options['filters']['status_node']['group'] = 1;
$handler->display->display_options['filters']['status_node']['expose']['operator'] = FALSE;
/* Display: Page */
$handler = $view->new_display('page', 'Page', 'page');
$handler->display->display_options['path'] = 'comments';
Create a view.
View cab get comments and you can add a filter to get only comments added by specific user, by passing (current) user id.
It's not a standard function, but this gives the logged in user's comments. Get the nid from the node table w.r.t to user id.
I get the logged-in user comments without any theme:
$nid_result = db_select('node') -> fields('node', array('nid')) ->
condition('uid', $puser->uid, '=') ->
condition('type', 'user_profile_comment', '=') -> execute();
$nid = $nid_resul t-> fetchCol();
$user_detail = array();
$result = db_select('comment') -> fields('comment', array('cid')) ->
condition('nid', $nid[0], '=') -> orderBy('cid', 'DESC') -> execute();
$cids = $result->fetchCol();
$comments=comment_load_multiple($cids);
print_r($comments);
I got the following problem. As a logged in user and administrator I'm able to see the view, but as anonymous user the view with products inside has disappeared.
In the view I got no permissions set and on the admin/people/permissions everything is normal. Does anybody know where I'm failing?
Here is my export of the VIEW:
$view = new view();
$view->name = 'frontpage';
$view->description = 'Emulates the default Drupal front page; you may set the default home page path to this view to make it your front page.';
$view->tag = 'default';
$view->base_table = 'node';
$view->human_name = 'Front page';
$view->core = 0;
$view->api_version = '3.0';
$view->disabled = FALSE; /* Edit this to true to make a default view disabled initially */
/* Display: Master */
$handler = $view->new_display('default', 'Master', 'default');
$handler->display->display_options['use_more_always'] = FALSE;
$handler->display->display_options['access']['type'] = 'none';
$handler->display->display_options['cache']['type'] = 'none';
$handler->display->display_options['query']['type'] = 'views_query';
$handler->display->display_options['query']['options']['query_comment'] = FALSE;
$handler->display->display_options['exposed_form']['type'] = 'basic';
$handler->display->display_options['pager']['type'] = 'full';
$handler->display->display_options['pager']['options']['items_per_page'] = '8';
$handler->display->display_options['pager']['options']['offset'] = '0';
$handler->display->display_options['pager']['options']['id'] = '0';
$handler->display->display_options['pager']['options']['quantity'] = '9';
$handler->display->display_options['style_plugin'] = 'grid';
$handler->display->display_options['row_plugin'] = 'fields';
/* Relationship: Content: Referenced products */
$handler->display->display_options['relationships']['field_product_product_id']['id'] = 'field_product_product_id';
$handler->display->display_options['relationships']['field_product_product_id']['table'] = 'field_data_field_product';
$handler->display->display_options['relationships']['field_product_product_id']['field'] = 'field_product_product_id';
$handler->display->display_options['relationships']['field_product_product_id']['label'] = 'Products';
/* Field: Commerce Product: Images */
$handler->display->display_options['fields']['field_images']['id'] = 'field_images';
$handler->display->display_options['fields']['field_images']['table'] = 'field_data_field_images';
$handler->display->display_options['fields']['field_images']['field'] = 'field_images';
$handler->display->display_options['fields']['field_images']['relationship'] = 'field_product_product_id';
$handler->display->display_options['fields']['field_images']['label'] = '';
$handler->display->display_options['fields']['field_images']['alter']['make_link'] = TRUE;
$handler->display->display_options['fields']['field_images']['element_type'] = 'div';
$handler->display->display_options['fields']['field_images']['element_label_colon'] = FALSE;
$handler->display->display_options['fields']['field_images']['element_wrapper_type'] = '0';
$handler->display->display_options['fields']['field_images']['element_default_classes'] = FALSE;
$handler->display->display_options['fields']['field_images']['click_sort_column'] = 'fid';
$handler->display->display_options['fields']['field_images']['settings'] = array(
'image_style' => 'medium',
'image_link' => '',
);
$handler->display->display_options['fields']['field_images']['delta_offset'] = '0';
$handler->display->display_options['fields']['field_images']['field_api_classes'] = TRUE;
/* Field: Content: Title */
$handler->display->display_options['fields']['title']['id'] = 'title';
$handler->display->display_options['fields']['title']['table'] = 'node';
$handler->display->display_options['fields']['title']['field'] = 'title';
/* Field: Commerce Product: Price */
$handler->display->display_options['fields']['commerce_price']['id'] = 'commerce_price';
$handler->display->display_options['fields']['commerce_price']['table'] = 'field_data_commerce_price';
$handler->display->display_options['fields']['commerce_price']['field'] = 'commerce_price';
$handler->display->display_options['fields']['commerce_price']['relationship'] = 'field_product_product_id';
$handler->display->display_options['fields']['commerce_price']['label'] = '';
$handler->display->display_options['fields']['commerce_price']['element_type'] = '0';
$handler->display->display_options['fields']['commerce_price']['element_label_colon'] = FALSE;
$handler->display->display_options['fields']['commerce_price']['element_wrapper_type'] = '0';
$handler->display->display_options['fields']['commerce_price']['element_default_classes'] = FALSE;
$handler->display->display_options['fields']['commerce_price']['click_sort_column'] = 'amount';
$handler->display->display_options['fields']['commerce_price']['settings'] = array(
'calculation' => '0',
);
$handler->display->display_options['fields']['commerce_price']['field_api_classes'] = TRUE;
/* Field: Commerce Product: Add to Cart form */
$handler->display->display_options['fields']['add_to_cart_form']['id'] = 'add_to_cart_form';
$handler->display->display_options['fields']['add_to_cart_form']['table'] = 'views_entity_commerce_product';
$handler->display->display_options['fields']['add_to_cart_form']['field'] = 'add_to_cart_form';
$handler->display->display_options['fields']['add_to_cart_form']['relationship'] = 'field_product_product_id';
$handler->display->display_options['fields']['add_to_cart_form']['label'] = '';
$handler->display->display_options['fields']['add_to_cart_form']['element_label_colon'] = FALSE;
$handler->display->display_options['fields']['add_to_cart_form']['show_quantity'] = 0;
$handler->display->display_options['fields']['add_to_cart_form']['default_quantity'] = '1';
$handler->display->display_options['fields']['add_to_cart_form']['combine'] = 1;
$handler->display->display_options['fields']['add_to_cart_form']['display_path'] = 0;
$handler->display->display_options['fields']['add_to_cart_form']['line_item_type'] = 'product';
/* Sort criterion: Content: Sticky */
$handler->display->display_options['sorts']['sticky']['id'] = 'sticky';
$handler->display->display_options['sorts']['sticky']['table'] = 'node';
$handler->display->display_options['sorts']['sticky']['field'] = 'sticky';
$handler->display->display_options['sorts']['sticky']['order'] = 'DESC';
/* Sort criterion: Content: Post date */
$handler->display->display_options['sorts']['created']['id'] = 'created';
$handler->display->display_options['sorts']['created']['table'] = 'node';
$handler->display->display_options['sorts']['created']['field'] = 'created';
$handler->display->display_options['sorts']['created']['order'] = 'DESC';
/* Filter criterion: Content: Type */
$handler->display->display_options['filters']['type']['id'] = 'type';
$handler->display->display_options['filters']['type']['table'] = 'node';
$handler->display->display_options['filters']['type']['field'] = 'type';
$handler->display->display_options['filters']['type']['value'] = array(
'product_display' => 'product_display',
);
$handler->display->display_options['filters']['type']['group'] = 1;
/* Filter criterion: Content: Published */
$handler->display->display_options['filters']['status']['id'] = 'status';
$handler->display->display_options['filters']['status']['table'] = 'node';
$handler->display->display_options['filters']['status']['field'] = 'status';
$handler->display->display_options['filters']['status']['value'] = '1';
$handler->display->display_options['filters']['status']['group'] = 1;
$handler->display->display_options['filters']['status']['expose']['operator'] = FALSE;
/* Filter criterion: Content: Promoted to front page */
$handler->display->display_options['filters']['promote']['id'] = 'promote';
$handler->display->display_options['filters']['promote']['table'] = 'node';
$handler->display->display_options['filters']['promote']['field'] = 'promote';
$handler->display->display_options['filters']['promote']['value'] = '1';
$handler->display->display_options['filters']['promote']['group'] = 1;
$handler->display->display_options['filters']['promote']['expose']['operator'] = FALSE;
/* Display: Page */
$handler = $view->new_display('page', 'Page', 'page');
$handler->display->display_options['path'] = 'frontpage';
/* Display: Feed */
$handler = $view->new_display('feed', 'Feed', 'feed');
$handler->display->display_options['defaults']['title'] = FALSE;
$handler->display->display_options['title'] = 'Front page feed';
$handler->display->display_options['pager']['type'] = 'some';
$handler->display->display_options['style_plugin'] = 'rss';
$handler->display->display_options['row_plugin'] = 'node_rss';
$handler->display->display_options['path'] = 'rss.xml';
$handler->display->display_options['displays'] = array(
'default' => 'default',
'page' => 'page',
);
$handler->display->display_options['sitename_title'] = '1';
$translatables['frontpage'] = array(
t('Master'),
t('more'),
t('Apply'),
t('Reset'),
t('Sort by'),
t('Asc'),
t('Desc'),
t('Items per page'),
t('- All -'),
t('Offset'),
t('« first'),
t('‹ previous'),
t('next ›'),
t('last »'),
t('Products'),
t('Title'),
t('Page'),
t('Feed'),
t('Front page feed'),
);
I've had this issue on a site I developed in the past. Drupal Commerce seems to be fussy when it comes to permissions.
I know of two routes you can follow to resolve this:
Edit your view. Under "Advanced", select "Query Settings", and tick "Disable SQL rewriting". Only do this if the view doesn't display sensitive information. Make sure you add a filter that checks to see if "Commerce Product: Status" is true, or you'll display unpublished products
Give the Anonymous user the "View any product of any type" permission.
i have problem with managing advanced custom fields (i have 4 of them). I want to check if some of them are empty when i add new post. I tried something like empty($_POST['name_of_post_meta']) but it does not work.
How can i catch
function wpse120996_add_custom_field_automatically($post_id)
{
global $wpdb;
if (!wp_is_post_revision($post_id)) {
$category = get_the_category($post_id);
$category = $category[0]->name;
$link = get_permalink($post_id);
if (($_POST['post_status'] == 'publish') && ($_POST['original_post_status'] != 'publish')) { // new post
$lector = get_post_meta($post_id, 'lektor_pl', true);
$subs_pl = get_post_meta($post_id, 'napisy_pl', true);
$orginal = get_post_meta($post_id, 'wersja_eng', true);
$subs_eng = get_post_meta($post_id, 'subs_eng', true);
if (empty($_POST['lektor_pl'])) {
$lector = 0;
}
if (empty($_POST['napisy_pl'])) {
$subs_pl = 0;
}
if (empty($_POST['wersja_eng'])) {
$orginal = 0;
}
if (empty($_POST['subs_eng'])) {
$subs_eng = 0;
}
if (!empty($_POST['lektor_pl'])) {
$lector = 1;
}
if (!empty($_POST['napisy_pl'])) {
$subs_pl = 1;
}
if (!empty($_POST['wersja_eng'])) {
$orginal = 1;
}
if (!empty($_POST['subs_eng'])) {
$subs_eng = 1;
}
$sql = $wpdb->prepare("INSERT INTO `wp_cron_notification` (`id`, `post_id`, `subb_pl` , `lector`, `subb_eng`, `orginal`,`link`, `category`) values (NULL, %s, %s, %s, %s, %s, %s, %s)", $post_id, $subs_pl, $lector, $subs_eng, $orginal, $link, $category);
$wpdb->query($sql) or die("ERROR #3");
}
}
}
add_action('wp_insert_post', 'wpse120996_add_custom_field_automatically', 1);
Thanks in advance for any help.
You can make use here of the ACF save_post function (http://www.advancedcustomfields.com/resources/acfsave_post/) which fires just after a post is saved. See more here:
If you put the following in your functions.php file you should be able to access the newly saved post and carry out code above. For example:
add_action('acf/save_post', 'post_save_update_title', 20);
function post_save_update_title($post_id){
//Your code here
}
So i am using a custom Login form with php using Sessions i would like to know how i can implement this into wordpress.
I have changed the database object to wpdb method successfully however i am now wondering how to implement the session on wordpress?
Is there a specific way or can i just create a session variable and check for that variable isset on the redirect page?
This is my current php login code without wpdb keep in mind i wont be using the classes:
$database = new MySQL();
$session = new Session($database);
$msg = '';
$msgl = '';
$msgemail = '';
$message = '';
if (isset($_POST['login']) && $session->isLogged == false) {
$email = $database->escapeString($_POST['user_email']);
$pass = sha1($database->escapeString($_POST['user_pass']));
$result = $database->executeQuery("SELECT user_id, user_type from `user-tbl` WHERE `billing_email`='$email' AND `account_password`='$pass'");
if ($database->numRows($result) == 1) {
$row = $database->fetchResult($result);
$_SESSION['account_id'] = $row['user_id'];
$_SESSION['account_type'] = $row['user_type'];
$_SESSION['security'] = hash('md5', $_SERVER['HTTP_USER_AGENT']);
if (isset($_POST['store'])) {
$_SESSION['store'] = 1;
$session->storeCookie();
} else {
$_SESSION['store'] = 0;
}
$location = 'Location: ';
if ($row['user_type'] == "customer") {
$location .= '/account.php';
} elseif ($row['user_type'] == "admin") {
$location .= '/admin/account.php';
} elseif ($row['user_type'] == "employee") {
$location .= '/employee/account.php';
}
header($location, true);
} else {
$msg = "Please Enter Correct E-mail OR Password.";
}
}
Thanks for your input!
I need to print a simple data from database to a particular block, I have used the code given below and got the text out put but it is not located in the block specified(hello_world).
function hello_world_block_view($delta = '') {
$block = array();
if ($delta == 'hello_world') {
$sql = "SELECT Test_name FROM Test_table";
$result = db_query($sql);
$record = $result->fetch();
foreach ($record as $records) {
echo $records;
}
$block['subject'] = t('Hello world Subject');
$block['content'] = t('Need to print database content');
}
return $block;
}
You need to connect variable $records with $block['content']. So it can looks like:
function hello_world_block_view($delta = '') {
$block = array();
if ($delta == 'hello_world') {
$output = '';
$sql = "SELECT Test_name FROM Test_table";
$result = db_query($sql);
$record = $result->fetch();
foreach ($record as $records) {
$output .= $records;
}
$block['subject'] = t('Hello world Subject');
$block['content'] = $output;
}
return $block;
}