Your current code relies on time travel to work:
$IDOutsideLoop = $post->ID;
global $post;
Specifically the first line is peering into the future, how can it know what $post->ID
is if it’s undeclared? The fix is global $post;
, but when the computer gets to that line, it’s too late
global $post;
$IDOutsideLoop = $post->ID;
If you want to list posts, you should consider WP_Query
or get_posts
. It may be faster in your case to use pre_get_posts
Related Posts:
- wp_get_archive for category returning different URLs on different but similar sites
- Where to insert redirect code based on http_referer?
- Add class to website based on post taxonomy
- Form Submission Not Working In Custom Theme
- Need php code’s output to display underneath a Divi Module
- Where is the PHP code generating an element?
- How do I check or test a WordPress Auto Year Change Script?
- Parse error: syntax error, unexpected ‘,’
- Prevent redirect to wp-login.php
- Extending WP_Customize_Media_Control to return filename
- Insert page content into another page with a changed variable
- Decimal values via get_option() with custom local (decimal point = comma)
- WP_Query | IF within $args array | Help me only add specific arguments if the argument is not blank
- Noindex Posts From Certain Authors In WordPress
- In WordPress, how do I get the number of posts next to single_cat_title(”); in the category.php file?
- Use get() method to grab all categories and output inside another method
- Seo Friendly Filter URLs
- Include only posts with meta key “add_in_rss” in RSS Feed
- Pass Variables or Variable Place-Holder from Editor to PHP
- Convert a string into a decimal number for use in query
- Is there a built-in function to generate multiple paragraph tags based on a string with new line separators?
- Issue with custom loop in Archive page
- Use custom template on custom post type
- How to display SQL query that ran in WC_Order_Query?
- Site downloads files instead of loading them
- get_the_ID() doesnt work
- How to get specific attribute from DB
- Saving data from custom form in wordpress database
- PHP contact form not redirecting [closed]
- WooCommerce pages looks unstyled
- customize woocommerce templates and display store on home
- Add value to new attribute inside WordPress menu items
- Do I always need to call wp_get_current_user() before accessing $current_user?
- class ‘wphpc_PAnD’ not found
- Please show me how to use an if/else statement in combination with wp_enqueue
- How to make function appear in sentence?
- Accessing values from ACF sub field (flexible content area) in PHP
- Save html content of a widget textarea
- Creating an image from a custom field
- Widgets not showing on custom theme
- How to disable delivery on certain date at checkout page in WooCommerce?
- How to add no follow to specific links?
- Loop over Array and get the distinct ids
- WordPress transient not working with WP Engine
- How to define a variable already defined in a plugin function?
- Basic wpdb update question
- Plugin development and composer
- WC_Customer delete function returns error
- Only show size attributes in product box woocommerce when available
- Array to string conversion on array_map
- Store metakey value as an array
- Increment paged on WP_Query
- Target post_date_gmt if it’s empty or not set
- How do I change this function from two returns to one string to show sku and dimensions in WooCommerce cart?
- Can I set up a hover animation in CSS depending on a PHP conditional?
- $_SESSION variables work perfectly on my local machine but returns NULL on development webserver
- Remove page title in product page using a function
- woocommerce specific quantities for product
- WP check_ajax_referer() is not working
- How to end a while loop when used in a fetch assoc function [closed]
- After adding my website to a new server, I keep getting a unexpected end of file error, but the file is identitcal to it’s original source
- How to access or parse key/values that have “string”
- If Array Values Match Another Array’s Values, Then
- Custom Post type slider with thumbnail navigation
- Show current user posts only
- file_exists function does not work
- How to Schedule Cronjobs for start of every month and year
- Does get_post function counts as view?
- Get ACF options field in PHP acting as CSS
- Hide posts if user is added to it WP_query
- Permalinks are not working in WordPress in digitalocean
- Can I add the post featured image to a specific RSS feed?
- Where can I find the inline-css of my theme header?
- Capture form data in one function and use it in another function (same file)
- User Meta Value not echoing despite Var_Dump Showing correct string
- Remove empty terms from array, sort alphabetically, update back to repeating field
- Why my loop isn’t working? [closed]
- how to display this functions?
- how can i add class or span tag at sprintf?
- Call WC_Product get_price()?
- How to have different site identity logos on each page on Astra Theme [closed]
- How to use AJAX to call php page with parameter
- How can I hook into the wp_mail function used by BackWPup?
- Syntax issue on rest object’s callback
- is_page_template showing partial results
- Is this a correct usage of ob_start() in my WordPress project?
- My website is not showing Footer section
- Get related product based on subcategory selected from category id with ID 142
- WordPress loop by meta key that is an array? and how loop multiple arrays
- How do I get my Javascript scripts working?
- Adding regular php file to site
- Can not add admin notices from the edit_user_profile_update hook (notices not being displayed)?
- Get post categories
- Only show first image in foreach loop
- How to send a HTTP Post request using PHP Curl and WordPress
- How can I find the cause of a 500 server error?
- php preg_match with multiple patterns
- ACF & Bootstrap Gallery Shows Blank Slides [closed]
- Handling PHP/HTML inside the output of a shortcode function
- How to allow download url redirection only if user logged in WordPress site?