Skip to content

Commit

Permalink
Update reset/restart numbering all posts order.
Browse files Browse the repository at this point in the history
Use default WordPress front page order including sticky at the top, and all other post statuses.
  • Loading branch information
ve3 committed Dec 11, 2024
1 parent ad9a61a commit 866981b
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 50 deletions.
36 changes: 4 additions & 32 deletions App/Controllers/Admin/Plugin/Activate.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,38 +102,10 @@ protected function doActivateAction()
// the example code above will not be use as explained.

// add order number into `posts` table.
$results = $wpdb->get_results(
'SELECT ' .
'`ID`, ' .
'`post_date`, ' .
'`post_name`, ' .
'`post_status`, ' .
'`menu_order`, ' .
'`post_type`' .
' FROM `' . $wpdb->posts . '`' .
' WHERE `' . $wpdb->posts . '`.`post_type` = \'post\'' .
' AND `' . $wpdb->posts . '`.`post_status` IN(\'' . implode('\', \'', $this->allowed_order_post_status) . '\')' .
' ORDER BY `' . $wpdb->posts . '`.`post_date` ASC',// get the oldest for number 1st to display at last page.
// the scheduled for future post has the `post_date` as scheduled date.
OBJECT
);
if (is_array($results)) {
$i_count = 1;
foreach ($results as $row) {
if ($row->menu_order == '0') {
$wpdb->update(
$wpdb->posts,
['menu_order' => $i_count],
['ID' => $row->ID],
['%d'],
['%d']
);
}
$i_count++;
}// endforeach;
unset($i_count, $row);
}
unset($results);
\RdPostOrder\App\Libraries\Input::static_setPaged();
$PostOrderM = new \RdPostOrder\App\Models\PostOrder();
$PostOrderM->resetAllPostsOrder(true);
unset($PostOrderM);

// add option related to this plugin (if not exists).
$plugin_option = get_option($this->main_option_name);
Expand Down
21 changes: 3 additions & 18 deletions App/Controllers/Admin/Posts/ReOrderPostsAjax.php
Original file line number Diff line number Diff line change
Expand Up @@ -353,24 +353,9 @@ public function ajaxResetAllPostsOrder()
}

\RdPostOrder\App\Libraries\Input::static_setPaged();
global $wpdb;

// get all posts order by current menu_order (even it contain wrong order number but keep most of current order).
$sql = 'SELECT `ID`, `post_date`, `post_status`, `menu_order`, `post_type` FROM `' . $wpdb->posts . '`'
. ' WHERE `post_type` = \'' . \RdPostOrder\App\Models\PostOrder::POST_TYPE . '\''
. ' AND `post_status` IN(\'' . implode('\', \'', $this->allowed_order_post_status) . '\')'
. ' ORDER BY `post_date` DESC';
$result = $wpdb->get_results($sql, OBJECT_K);
unset($sql);
if (is_array($result)) {
$i_count = count($result);
foreach ($result as $row) {
$wpdb->update($wpdb->posts, ['menu_order' => $i_count], ['ID' => $row->ID], ['%d'], ['%d']);
$i_count--;
}
unset($i_count, $row);
}
unset($result);
$PostOrderM = new \RdPostOrder\App\Models\PostOrder();
$PostOrderM->resetAllPostsOrder();
unset($PostOrderM);

// done update menu_order numbers
$output['form_result_class'] = 'notice-success';
Expand Down
86 changes: 86 additions & 0 deletions App/Models/PostOrder.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,92 @@ public function getLatestMenuOrder()
}// getLatestMenuOrder


/**
* Reset or restart all posts order to be based on WordPress default post order on front page.
*
* @since 1.0.5
* @global \wpdb $wpdb WordPress DB class.
* @param bool $updateOnlyMenuOrderZero Set to `true` to update only if menu order is zero. This is good for first activation that prevent a mess while deactivate then activate. Default is `false`.
* @return int|false Return `false` on failure, return number of rows found and updated which can be zero.
*/
public function resetAllPostsOrder($updateOnlyMenuOrderZero = false)
{
global $wpdb;
// get sticky posts by its default order based on front page. ---------------
$stickies = get_option('sticky_posts');
if (!empty($stickies)) {
$stickiesPlaceholders = array_fill(0, count($stickies), '%d');
$sql = 'SELECT `ID`, `menu_order` FROM `' . $wpdb->posts . '` WHERE `ID` IN (' . implode(', ', $stickiesPlaceholders) . ')';
$sql .= ' AND `post_status` IN(\'' . implode('\', \'', $this->allowed_order_post_status) . '\')';
$sql .= ' ORDER BY `post_date` DESC';
$prepared = $wpdb->prepare($sql, $stickies);
$stickyPosts = $wpdb->get_results($prepared);
unset($prepared, $sql);
}
unset($stickies);
// end get sticky posts by its default order based on front page. -----------

// get all the rest posts by its default order based on front page. ----------
$sql = 'SELECT `ID`, `menu_order` FROM `' . $wpdb->posts . '` WHERE ';
$sql .= ' `post_type` = %s';
$sql .= ' AND `post_status` IN(\'' . implode('\', \'', $this->allowed_order_post_status) . '\')';
if (!empty($stickiesPlaceholders) && isset($stickyPosts) && is_array($stickyPosts) && !empty($stickyPosts)) {
$sql .= ' AND `ID` NOT IN (' . implode(', ', $stickiesPlaceholders) . ')';
}
$sql .= ' ORDER BY `post_date` DESC';
$values = [\RdPostOrder\App\Models\PostOrder::POST_TYPE];
if (isset($stickyPosts) && is_iterable($stickyPosts)) {
foreach ($stickyPosts as $stickyPost) {
$values[] = $stickyPost->ID;
}// endforeach; stickies
unset($stickyPost);
}// endif;
$prepared = $wpdb->prepare($sql, $values);
$postsResult = $wpdb->get_results($prepared);
unset($prepared, $sql, $values);
// end get all the rest posts by its default order based on front page. ------

// merge sticky posts to the beginning of before normal posts.
if (isset($stickyPosts) && is_array($stickyPosts)) {
$allPosts = array_merge($stickyPosts, $postsResult);
} else {
$allPosts = $postsResult;
}
unset($postsResult, $stickyPosts);

if (!is_array($allPosts)) {
return false;
}

$i_count = count($allPosts);
$updated = 0;
foreach ($allPosts as $row) {
if (
(
true === $updateOnlyMenuOrderZero && '0' === strval($row->menu_order)
) ||
false === $updateOnlyMenuOrderZero
) {
$updateResult = $wpdb->update(
$wpdb->posts,
['menu_order' => $i_count],
['ID' => $row->ID],
['%d'],
['%d']
);
if (false !== $updateResult) {
++$updated;
}
}// endif; update only menu order is 0.
--$i_count;
}// endforeach; all posts
unset($i_count, $row);
unset($allPosts);

return $updated;
}// resetAllPostsOrder


/**
* Set `menu_order` column on `posts` table to zero (its default value).
*
Expand Down

0 comments on commit 866981b

Please sign in to comment.