acf/save_post not triggered until manual update - advanced-custom-fields

I'm using acf_form to create a post like this:
<?php acf_form(array(
'post_id' => 'new_post',
'post_title' => true,
'new_post' => array(
'post_type' => 'books',
'post_status' => 'publish'
),
'fields' => array('field_5fb2a66de25ba', 'field_5fb009504f235',),
'submit_value' => 'Create Book',
'html_submit_button' => '<input type="submit" value="%s" />',
'updated_message' => ("Book submitted",)
Everything works except my acf/save_post function does not trigger until I manually edit the post and update it. Here's what I'm doing in acf/save_post:
$key_word = get_field('field_5fb2a66de25ba', $post_id);
update_post_meta( $post_id, 'rank_math_focus_keyword', $key_word );
This works, but not until I manually edit and update the page.
I'm not the only one with this question. If I get an answer, I'll find the others and share the good news.
Cheers, Richard

Eureka! All I had to do was change the priority on my action:
add_action('acf/save_post', 'my_acf_save_post', 25);

Related

Display previous post instead of the latest post

On wordpress I have a littl box that displays the latest post from a category.
The code looks like this:
<?php $args = array(
'posts_per_page' => 1,
'order' => 'DESC',
'order_by' => 'date',
'category_name' => 'updates'
If I wanted to create a second box that displays not the latest post but the one before how should I approach it?
Thanks.
you can use get_posts() function like this:
$posts = get_posts(array(
'posts_per_page' => 2,
'order' => 'DESC',
'order_by' => 'date',
'category_name' => 'updates'
));
and you can access the second post with $posts[1].
Thank you. Eventually I got what I wanted with the following:
<?php $args = array(
'posts_per_page' => 1,
'order' => 'DESC',
'order_by' => 'date',
'category_name' => 'updates',
'limit' => '1',
'offset' => '1'
For the following boxes it becomes:
'offset' => '2' and 'offset' => '3'

ZF2 - inputFilter -> set value to null

In my ZF2 app I've got a form with several optional fields. If user decide to leave that field blank then it should set it as NULL in db, however - it doesn't work.
Example field:
$this->add(array(
'name' => 'productId',
'type' => 'select',
'options' => array(
'value_options' => array(
'global' => 'Global option', // this should be null
'mobile' => 'Another option'
)
),
));
And filter:
$inputFilter->add(array(
'name' => 'productId',
'validators' => array(
array(
'name' => 'Callback',
'options' => array(
// 'messages' => array(
// \Zend\Validator\Callback::INVALID_VALUE => $value,
// ),
'callback' => function ($value) {
if($value === 'global')
{
$value = null;
//return true;
// echo '<pre>' . var_export($value, true) . '</pre>';
// die();
}
}
),
),
),
));
This piece of code works when "mobile" option is selected. However, with 'global' option it doesn't work.
As you can see, I've done some basic debugging to make sure that value is overrided on callback and it indeed returns NULL. Regardless of that validator says:
array (
'productId' =>
array (
'callbackValue' => 'The input is not valid',
),
)
So I tried to return true on callback which resulted in below error:
Statement could not be executed (23000 - 1452 - Cannot add or update a child row: a foreign key constraint fails (app.codes, CONSTRAINT fk_products_id FOREIGN KEY (productId) REFERENCES products (id)))
How am I supposed to pass null param to my db? If I cut this field completly from form then it is ignored and everything works.
Found solution, it's pretty simple actually.
I can set desired select options to some specific value and then use ToNull filter to convert it into null -
https://framework.zend.com/manual/2.4/en/modules/zend.filter.null.html
Code for $inputFilter:
$inputFilter->add(array(
'name' => 'productId',
'required' => false,
'filters' => array(
array('name' => 'ToNull', 'options' => array('type' => \Zend\Filter\ToNull::TYPE_STRING)),
),
));

Call to undefined method Varien_Db_Statement_Pdo_Mysql::addColumn()

When i try to run my upgrade script it gives Call to undefined method Varien_Db_Statement_Pdo_Mysql::addColumn() error on second addcolumn but when i remove all other column and keep only one addColumn it work fine.My upgrade script is as below
$installer->startSetup();
/**
* alter table 'savecart/savecart'
*/
$installer->getConnection()
->addColumn($installer->getTable('savecart/savecart'),'savecart_name', array(
'type' => Varien_Db_Ddl_Table::TYPE_TEXT,
'nullable' => true,
'length' => 255,
'comment' => 'Savecart Name'
))
->addColumn($installer->getTable('savecart/savecart'),'savecart_comment', array(
'type' => Varien_Db_Ddl_Table::TYPE_TEXT,
'nullable' => true,
'comment' => 'Savecart Comment'
))
->addColumn($installer->getTable('savecart/savecart'),'savecart_bill_id', array(
'type' => Varien_Db_Ddl_Table::TYPE_INTEGER,
'nullable' => true,
'length' => 10,
'comment' => 'Billing Id'
))
->addColumn($installer->getTable('savecart/savecart'),'savecart_valid_till', array(
'type' => Varien_Db_Ddl_Table::TYPE_DATE,
'nullable' => true,
'comment' => 'Valid Till Date'
));
$installer->endSetup();
CHAINING CORRECTLY
I was looking into this and found several sites that use a different writing method:
http://magedevguide.com/challenge/chapter4/1
http://magento.ikantam.com/qa/how-setup-magento-scripts
Please try these methods with your values..
You can try copy pasting following code, but please review it for your values..
$installer->startSetup();
/**
* alter table 'savecart/savecart'
*/
$installer->getConnection()
->newTable($installer->getTable('savecart/savecart'))
->addColumn('savecart_name', Varien_Db_Ddl_Table::TYPE_TEXT, 255, array('nullable' => true), 'Savecart Name')
->addColumn('savecart_comment', Varien_Db_Ddl_Table::TYPE_TEXT, null, array('nullable' => true), 'Savecart Comment')
->addColumn('savecart_bill_id', Varien_Db_Ddl_Table::TYPE_INTEGER, 10, array('nullable' => true), 'Billing Id')
->addColumn('savecart_valid_till', Varien_Db_Ddl_Table::TYPE_DATE, null, array('nullable' => true), 'Valid Till Date');
$installer->endSetup();
Please let me know if one of the solutions is working so I can update my answer.
Please use following upgrade script add multiple columns to the existing magento table.for detailed explanation.
https://www.pearlbells.co.uk/upgrade-script-multiple-columns-magento/
$installer->getConnection()
->addColumn($installer->getTable('imagedetail'),'filters_workshop_colour',
array(
'type' => Varien_Db_Ddl_Table::TYPE_TEXT,
'nullable' => true,
'comment' => 'Filters Workshop Colour'
));
$installer->getConnection()
->addColumn($installer->getTable('imagedetail'),'splashbackcolor',
array(
'type' => Varien_Db_Ddl_Table::TYPE_TEXT,
'nullable' => true,
'comment' => 'Splash Back Color'
));
$installer->getConnection()
->addColumn($installer->getTable('imagedetail'),'cabinetcolor',
array(
'type' => Varien_Db_Ddl_Table::TYPE_TEXT,
'nullable' => true,
'comment' => 'Cabinet Color'
));

Complex CakePHP query refactoring

I have just inherited a project and I need to re-work a bit of code so that the pagination of an infinite scroller still work properly.
Right now the code is grabbing all of the categories and their products, and listings. I need to edit it so that only categories that have, products, which have active listings are returned.
Here is the code that eventually worked:
$catData = $this->find('all',array(
'conditions' => array(
'Indexer.type' => 'Category',
'Listing.listing_id IS NOT NULL'
),
'joins' => array(
array('table' => 'peeka_product_category_link',
'alias' => 'Link',
'type' => 'LEFT',
'conditions' => array(
'Link.category_id = Category.category_id'
)
),
array('table' => 'peeka_products',
'alias' => 'Product',
'type' => 'LEFT',
'conditions' => array(
'Product.product_id = Link.product_id'
)
),
array('table' => 'peeka_listings',
'alias' => 'Listing',
'type' => 'LEFT',
'conditions' => array(
'Listing.product_id = Product.product_id',
'Listing.listing_end_date >=' => $date,
'Listing.listing_start_date <=' => $date,
"Listing.listing_status = 'Active'"
)
),
),
'order' => 'Category.category_name ASC',
'limit' => $set_limit,
'fields' => array('Category.category_id, Category.category_name, Indexer.url'),
'group' => 'Category.category_id',
'recursive' => 0
));
EDIT: Thanks to Dave this is working now and I just wanted to post it for future reference. Maybe it will help someone else.
"...only categories that have, products, which have active listings are
returned."
"...a way to combine these three queries into one, so that the first
Category->find() function retrieves all the valid data."
To retrieve data and restrict it based on fields of associated models, you'll want to use CakePHP's JOIN.
It's hard to answer your question further than that without just writing the code for you, so - give JOINs a try, then come back and ask another more specific question if you have any issues with it.

Wordpress Multiple meta_key (WPDB or WP_Query)

Attempting to test whether a post has a 'main_slider', 'flickr-slider', or 'video-slider' value. 'main_slider' is a String, 'flickr-slider' and 'video-slider' are both Boolean.
This is what I have so far, which doesn't work a lick...
$slider = new WP_Query(
array(
'ignore_sticky_posts' => 1,
'post_type' => 'any',
'orderby' => 'date',
'nopaging' => true,
'posts_per_page' => 10,
'meta_query' => array(
'relation' => 'OR',
array(
'key' => 'main_slider'
),
array(
'key' => 'flickr-slider'
),
array(
'key' => 'video-slider'
)
)
)
);
Thank you for any help in advance.
I decided to go with a workaround. I called each meta_key independently, merged while stripping out duplicate post, and then sorted by date. I'm sure its a heavier load on the server, but it got the job done.