Display previous post instead of the latest post - mysql

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'

Related

show Enable/disable value on grid list magento

In Magento 1.9, I have created Module and in admin grid i want to show value 0/1 as Enable/Disable title. But it is not working. Here is my script which i am using.
$this->addColumn('status',
array(
'header'=> Mage::helper('catalog')->__('Status'),
'align' =>'right',
'width' => '50px',
'index' => 'status',
'options' => array('1'=>'Enable','0'=>'Disable')
)
);
Please anybody can help me where i need to change in Grid.php file.
Thank you
$this->addColumn('is_active', [
'header'=> __('status'),
'align' => 'left',
'index' => 'is_active',
'params' => ['id'=>'getId'],
'type' => 'options',
'options' => array(
1 => 'enabled',
0 => 'disabled',
),
]);

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'
));

Linking two models with CakeDC Search Plugin

I have a small problem that is me consuming days.
I need made a search in two diferent models in CakePHP, i'm using CakeDC Search Plugin.The models are 'Desordens' and 'Sinonimos', the association is hasMany. One 'Desorden' hasMany 'Sinonymous':
public $hasMany = array(
'Sinonimo' => array(
'className' => 'Sinonimo',
'foreignKey' => 'desorden_id',
'dependent' => false,
'conditions' => '',
'fields' => '',
'order' => '',
'limit' => '',
'offset' => '',
'exclusive' => '',
'finderQuery' => '',
'counterQuery' => ''
),
So, i follow the example in CakeDC Docs an made this :
public $filterArgs = array(
'busca' => array(
'type' => 'like',
'encode' => true,
'before' => false,
'after' => false,
'field' => array(
'Desorden.name',
'Desorden.orphanumber',
'Sinonimo.sinonimo')
)
);
What i want to do is a search where the user can insert the name,orphanumber or the synonymous.In SQL would be:
SELECT `Desorden`.`id`, `Desorden`.`name`, `Desorden`.`orphanumber`, `Desorden`.`expertlink`, `Desorden`.`tipodesordem`, `Desorden`.`descricao`, `Sinonimo`.`id`, `Sinonimo`.`sinonimo`, `Sinonimo`.`desorden_id` FROM `rederaras`.`desordens` AS `Desorden` LEFT JOIN `rederaras`.`sinonimos` AS `Sinonimo` ON (`Sinonimo`.`desorden_id` = `Desorden`.`id`) WHERE ((`Desorden`.`name` LIKE 'mult') OR (`Desorden`.`orphanumber` LIKE 'mult') OR (`Sinonimo`.`sinonimo` LIKE 'mult'))
But the table 'Sinonimo' its not being joined and i have the 1054 error:
Error: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'Sinonimo.sinonimo' in 'where clause'
I don't know what i missing, the controller i have configured like this:
$this->Prg->commonProcess();
$this->Desorden->recursive = 0;
$this->Paginator->settings['conditions'] = $this->Desorden->parseCriteria($this->Prg->parsedParams());
$this->set('desordens', $this->Paginator->paginate());
I think its no problem in the model or cakedc filterArg configuration, because the query is constructed in the right way.I try to use bindModel to made a join between the tables 'Desordens' and 'Sinonimos' but no success....
$this->Desorden->bindModel(array(
'hasMany' => array(
'Sinonimo' => array(
'foreignKey' => false,
'conditions' => array('Desorden.id = Sinonimo.desorden_id')
)
)
), false);
Anyone can help me to made this join ?
Thanks
I solved the problem using $belongsTo in the two Models. When i set $belongsTo the CakeDC create a Join between the tables. I don't know if this a best solution, but works for me. If this help someone.

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.