Skip to content
Read For Learn
Read For Learn
  • Database
    • Oracle
    • SQL
  • C
  • C++
  • Java
  • Java Script
  • jQuery
  • PHP
Read For Learn
  • Database
    • Oracle
    • SQL
  • C
  • C++
  • Java
  • Java Script
  • jQuery
  • PHP

WP update_post_meta link loop

What you are trying to accomplish here is will not work just but calling the function make_sticky on a link because this function doesn’t actually print anything for the final user to take an action. What will happen here is that every post in the loop will get set as sticky every time that method is called.

So to resolve your issue you want to create a page within your WordPress administration and then create a page template inside your theme and assign it to that page.

For the sake of an explanation I will say that you called this page make-sticky, now you will access this page by creating a link like this:

<?php echo add_query_arg( array( '_post', get_the_ID() ), get_permalink( get_page_by_path( 'make-sticky' ) ) );

This will output http://yoursite.com/make-sticky/?_post=130.

The template file has to contain only the template metadata section:

<?php
/*
 * Template Name: Make Sticky
 * Description: This page will make the Post Sticky
 */

After that you will add the following lines on your functions.php:

<?php

function q166504_make_sticky_redirect(){
    // If we are not on that page template just leave
    if ( ! is_page_template( 'template-make-sticky.php' ) ){
        return;
    }

    // If the _post _GET param doesnt exist leave
    if ( empty( $_GET['_post'] ) ){
        exit( wp_redirect( home_url() ) );
    }

    $post_id = absint( $_GET['_post'] );

    // If the user doesn't have permission leave
    if ( ! current_user_can( 'edit_others_posts' ) ){
        exit( wp_redirect( get_permalink( $post_id ) ) );
    }

    // Safe to execute the action
    $is_sticky = (bool) get_post_meta( $post_id, 'sticky', 0 );

    // Make the toggle
    update_post_meta( $post_id, 'sticky', ! $is_sticky );

    // Now redirect back to the post
    exit( wp_redirect( get_permalink( $post_id ) ) );
}

add_action( 'template_redirect', 'q166504_make_sticky_redirect' );

This method will intercept the template of WordPress then redirect the user to edited post, if the user doesn’t have permission or something is wrong it will redirect to the home.

Related Posts:

  1. How to display Yoast SEO meta description in archive template for each post instead of the_excerpt()? [closed]
  2. Get ID of a page containing secondary loop in content
  3. How to add “time” data this?
  4. Next and Previous loop
  5. How to hook into container
  6. Output meta into arrays
  7. value of metadata is null wrong use of if statement
  8. Sending mail not working correctly
  9. using update_user_meta to rank users
  10. get_post_meta printing empty fields, but it shouldn’t be
  11. Check value of post meta within IF statement
  12. Best way to check if a post with specific meta exists
  13. Humanmade | Custom meta box class: How to display a repeatable meta box group?
  14. Display articles related to a custom field on a page
  15. How do I get the attributes of a short code from a post?
  16. WordPress category & taxonomy loop with pagination
  17. Changing behavior of the loop twice in one page
  18. Show scheduled posts in archive page
  19. post__in – Placing content from a foreach loop inside of an array
  20. loop query exclude meta_key with meta_value
  21. “Blog pages show at most” in The Loop
  22. wp_insert_posts Fatal error: Maximum function nesting level of ‘100’ reached, aborting!
  23. Post Loop not Returning Permalink
  24. Displaying child page content of a certain parent
  25. add_filter() doesn’t work in loop
  26. The Loop in Static Page
  27. Content/Excerpt length control for a specific loop?
  28. Why we use if with while loop?
  29. Loop posts only excluding first post
  30. Loop repeating design pattern
  31. Sort Posts By Category?
  32. Customize WooCommerce Product Images (Placement and size) [closed]
  33. Create a loop that gets pages with their template
  34. Why is my loop not dynamically grabbing the correct Category and displaying all categorized posts?
  35. How to check if a post exist?
  36. Query posts if meta key starts with
  37. 3 Posts in Loop, Show Stickies First
  38. Loop within a loop (Again) for template
  39. How to use current_post to open a new unordered list every five posts
  40. Why cant you place the Loop outside of the index.php?
  41. sticky post in custom loop
  42. Rating system and changing the loop
  43. Ajax Button to load more Posts into a timeline
  44. How to target thumbnails of the first post in the loop
  45. Showing sticky posts with get_posts()
  46. single.php – how to mark current page in the loop
  47. Get 1 more post in loop
  48. Pagination not working in category listing [duplicate]
  49. How to get the excerpt for is_home() outside of the loop isn’t working
  50. Category links including all posts
  51. Custom loop ordering not working
  52. First archive page with a few posts
  53. Hiding a row in the loop if empty
  54. Loop through ACF taxonomies and output associated posts
  55. How to get user ID’s from multiple usernames?
  56. get_delete_post_link() inside Loop stubbornly returns nothing
  57. Query posts only with actual text content (not including shortcode or images)
  58. Comments_template doesn’t work
  59. Get markup for post without loading into the template
  60. The loop starting at a certain ID
  61. How can this multiple loop have pagination as described?
  62. Default featured image set as background image [closed]
  63. Breaking up the results of a loop
  64. How do i create a list-posts-page?
  65. Exclude posts based on an array
  66. Is file_exists() compatible with timthumb.php? [closed]
  67. Using is_page_template inside loop
  68. Set depth of pages to fetch
  69. Trying to get all links in my posts
  70. Creating a User-Adjustable chart in WordPress
  71. Archive: Lists itself
  72. Please tell how to stop the category from showing up on posts
  73. How to show the last article in a different way in my blog?
  74. Reorder posts in a loop: have the posts by one particular author below the others
  75. How to execute a user loop with shortcode
  76. how to get author comment inside the loop?
  77. How to create loop in custom page, and get id from url into this loop?
  78. How to fix pagination for custom loops?
  79. Check to see if a field is within an array in twig
  80. Archive sorted by month – separate
  81. Posts are being displayed from old to new, in ascending order
  82. Loop in taxonomy for terms and post
  83. WordPress Tags in class
  84. The Loop isn’t working
  85. Changing the loop w/o killing category links
  86. Custom loop page with post navigation
  87. Pages with a loop (index, archive) are loading the first image as post_thumbnail
  88. the loop – how to control whether wp or plug-in runs it
  89. stuck with template hierarchy
  90. How to create an identical second loop for attachments?
  91. Modify my code – which takes the first sentence of the post and use it as a h2 tag – to work outside the loop
  92. How to show only specific tag in wordpress loop
  93. Different style for first two (sticky) posts
  94. WooCommerce – Checkout suddenly stops working [closed]
  95. pull 500 post of many from database [duplicate]
  96. Want to images load first then title in WordPress loop
  97. How to add a continuous number to HTML tag attribute value inside The Loop [closed]
  98. WordPress Not Sorting By Custom Field
  99. Landing page with login
  100. How can I create an entirely new, separate display of posts?
Categories loop Tags loop, post-meta
Featured Images missing on migrated site
Function/filter or plugin to change post status based on custom field value

Recommended Hostings

Cloudways: Realize Your Website's Potential With Flexible & Affordable Hosting. 24/7/365 Support, Managed Security, Automated Backups, and 24/7 Real-time Monitoring.

FastComet: Fast SSD Hosting, Free Migration, Hack-Free Security, 24/7 Super Fast Support, 45 Day Money Back Guarantee.

Recent Added Topics

  • Bug in translation system: load_theme_textdomain() returns true, files are available and accessible but the language defaults to english
  • Custom Elementor controls not appearing in the widget Advanced tab using injection hooks
  • Get the name of the template/*html file used
  • Trying to Add Paging to Single Post Page
  • Sharing media files between live and staging servers
  • How to display the description of a custom post type in the dashboard?
  • Critical error on image display
  • Copying WP data and files into new install?
  • How to determine the DirectAdmin WordPress backup date?
  • How to get list of ALL tables in the database?
© 2026 Read For Learn
  • Database
    • Oracle
    • SQL
  • algorithm
  • asp.net
  • assembly
  • binary
  • c#
  • Git
  • hex
  • HTML
  • iOS
  • language angnostic
  • math
  • matlab
  • Tips & Trick
  • Tools
  • windows
  • C
  • C++
  • Java
  • javascript
  • Python
  • R
  • Java Script
  • jQuery
  • PHP
  • WordPress