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. Wishlist Icon in Divi Blog module (loop)
  16. How to fix pagination for custom loops?
  17. Jquery Slider for profile template
  18. Counting the posts of a custom WordPress loop (WP_Query)?
  19. if ( is_home() && ! is_front_page() )
  20. Get excerpt using get_the_excerpt outside a loop
  21. Get post content from outside the loop
  22. Why should I put if(have_posts()), is while(have_posts()) not enough?
  23. Display featured products through custom loop in woocommerce on template page
  24. Split Content and Gallery
  25. How to get Author ID outside the loop
  26. How can i display the content in plaintext
  27. Redirect loop when trying to login to /wp-admin/ [duplicate]
  28. How to split a loop into multiple columns
  29. Is it necessary to reset the query after using get_posts()?
  30. Is there any difference between the_title() and echo get_the_title()?
  31. AJAX with loop filtering categories
  32. Do I need to use The Loop on pages?
  33. Remove the Homepage Query
  34. remove tags from the_content
  35. the_title() shows title of the first post instead of the page title?
  36. How to force excerpts / teasers in the loop
  37. Retrieve each widget separately from a sidebar
  38. Why am I being limited to ten posts on a custom loop?
  39. Should I use loop in the single.php file?
  40. A search for ‘0’ returns results
  41. Why do themes rely on “The Loop”?
  42. How to return loop contents
  43. Using the Loop to show all levels of subpages under a parent page? Halfway there
  44. Cleanest Way to Select Every Second Element in a Loop?
  45. Insert image or ad script after 3 posts using the loop
  46. Get date of last update outside of loop
  47. Pagination not working on home page
  48. Child Pages Loop
  49. How to place comments_template(); outside the loop?
  50. Multiple Loops Homepage?
  51. How to Change Loop to Order Posts by Views (using wp-postviews plugin)
  52. how to upload image using wp_handle_upload
  53. Avoiding using a loop to access a single post
  54. Strategy to get post meta for use outside the loop
  55. How to get the first image gallery of a product in woocommerce in a loop
  56. Get post by page name or slug
  57. save_post + insert_post = infinite loop
  58. Loop.php vs looping inside template file
  59. Custom Loop and Infinite Scroll
  60. Query Custom Meta Value with Increment
  61. Loop that displays PARENT PAGE & CHILD PAGE & outputs GRANDCHILD PAGE title and content
  62. is_home, and is_front_page conditional problem
  63. Is `query_posts` really slower than secondary query?
  64. Are there any scenarios where the query_posts may be used?
  65. the_content and wp_link_pages
  66. Adding ‘current_post_item’ class to current post in the loop
  67. How do I get the attributes of a short code from a post?
  68. How can I custom order the results from wp_list_categories?
  69. ACF Repeater loops and resets – where is the reset_rows() documentation? [closed]
  70. WooCommerce Product Page Loop – Output All Product Thumbnails
  71. wp-admin redirecting to https, denying login
  72. Endless loop with wp_insert_post and wp_update_post
  73. Display subpages under parent page as a list within a loop
  74. Why is it necessary to call rewind_posts() when using the loop more than once? [duplicate]
  75. When to use wp_reset_postdata();
  76. Is including the loop necessary for page.php? [duplicate]
  77. the_title(); works in a page template, outside the loop. Why?
  78. wordpress loop for specific category
  79. Add 20yrs to post date, and then query
  80. Why do themes have `while( have_posts() )` in templates like single.php?
  81. When and Why is is_singular(‘my_cpt’) true while in_the_loop() is false?
  82. Three different post types on homepage
  83. What is best way passing variables to theme templates and using them different places like widgets?
  84. Apply styling only to first page sticky posts
  85. WordPress category & taxonomy loop with pagination
  86. Add Incrementing ID to each paragraph in the_content
  87. Changing behavior of the loop twice in one page
  88. How do I prevent one of two multiple loops from repeating on a second page?
  89. Insert/sticky specific post into Loop at specific location
  90. Style every four posts differently [duplicate]
  91. Return vs Echo Shortcode
  92. wp_list_categories: get latest featured_image of category
  93. Hook into the loop via a plugin, and output something after every X post?
  94. Grossly inefficient wordpress loops!
  95. Multiple posts with one loop iteration
  96. Two custom loops, pagination, offset
  97. Is it possible to sort the post based on a custom field?
  98. Identify the page being shown while in The Loop
  99. WordPress Alphabetical Glossary close div in loop
  100. Query date in wordpress loop
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
  • Post Navigation Elementor
  • Custom Elementor controls not appearing in the widget Advanced tab using injection hooks
  • WPFacet multiple loop displaying duplicate content
  • Get the name of the template/*html file used
  • Fix: WordPress.DB.PreparedSQL.NotPrepared Plugin Check (PCP)
  • Removing unnecessary CSS and JS code from wp_head()
  • hexagonal image gallery. Am I on the right track? [closed]
  • Trying to Add Paging to Single Post Page
  • Sharing media files between live and staging servers
© 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