Call post-id from URL hash not working?

I'm trying to have a modal box open by default when calling it by its id. For example, if i enter the url www.mysite.com#post-28 (#post28 being the ID of the post) I will land with the moadal box and the content of the post-28. For this I have been using the following jquery ( iattach a basic version of the code): <script> jQuery(function ($) { function popModal() { { alert("Your book is overdue.") }; } var hash = window.location.hash; if …
Category: Web

get_the_ID() in the footer returns wrong value

I'm using get_the_ID() in footer.php but it returns a wrong value. It might come from any of the plugin which is altering The Loop without resetting it after with wp_reset_postdata() as mentioned in $post->ID and the_id() always return the wrong value. Indeed the page shows a list of recent posts, etc. so this might modify the current Loop. If I can't modify this (because of third party code), how to get the ID of the current post in the footer …
Category: Web

Wordpress get the child pages of current page

I want to display the child pages of the current page you are on. <?php wp_list_pages('child_of=67&sort_column=menu_order&title_li=') ?> If I give the id number like above it works perfectly like this: However I want to use this as a template for multiple pages, but when I use the ID or get Id or echoing the ID, it also take the sibling pages of the parent page. Which I don't need. <?php wp_list_pages('child_of=the_ID()&sort_column=menu_order&title_li=') ?> I can't figure it out what I am …
Category: Web

Why does my output of get_the_ID() change after a wp_query?

I use ACF. I have the following code: $args = array( "post_type" => "membership", "posts_per_page" => 1, 'meta_query' => array( 'relation' => 'AND', array( 'key' => 'kennel', 'value' => '"' . $toReturn["request"]["groupId"] . '"', 'compare' => "LIKE" ), array( 'key' => 'user', 'value' => $toReturn["userId"], 'compare' => "=" ) ) ); $the_query = new WP_Query( $args ); if ( $the_query->have_posts() ) { while ( $the_query->have_posts() ) { $the_query->the_post(); $mid = get_the_ID(); $totalRuns = uc_totalRuns($mid); $unprocessedRuns = uc_unprocessedRuns($toReturn["request"]["groupId"], $toReturn["userId"], "registered"); // …
Category: Web

How to redirect (301) trashed post to it's parent taxonomy TERM ARCHIVE instead of 404 page

I have a taxonomy called "Sports", that includes terms: Football, Rugby, Tennis... What I want to achieve is when (for instance) a post that has the term football is trashed and a visitor try access it, it will be redirected to (for SEO purpose): mywebsite.com/sports/football/ . I spent many days trying to figure how to do this but in vain. Here is an example of codes I tried: function redirect_trashed_posts(){ if( is_404() ){ global $wp_query, $wpdb, $post; $post_id = $post->ID; …
Category: Web

Reverse Cross-Sells (WooCommerce)

When you run get_post_meta( get_the_ID(), '_crosssell_ids',true); you get an array of all the products that the current product cross sells to. How do I reverse that? To get all the products that cross sells to the current item? Example: Not this: Product 1 -> Cross-sell: Product 2, Product 3 But this: Product 3 <- Cross-sell: Product 1
Category: Web

Global page ID variable empty error

I am creating theme and I have a need to create global variables within theme functions.php file. Currently I am doing it like this: /** * Setup globals */ global $nb_id, $nb_page; $nb_id = get_the_ID(); // Prepare & set fallback $globals_array = array( 'nb_id' => $nb_id, 'nb_page' => array( 'id' => $nb_id, 'end' => 'front', 'name' => 'home', 'type' => 'page' ) ); // Set each value from globals array as global $var; foreach ($globals_array as $name => $value) { …
Category: Web

Get the page ID if the page content has a shortcode

I have been trying to get the page ID of which it contains shortcode. I 'm getting null. Not sure what I'm doing wrong. function availabilty_id() { global $template_id; // Custom Query to look only in pages post type $template_query = new WP_Query( array( 'post_type' => 'page', ) ); if ( $template_query->have_posts() ) { while ( $template_query->have_posts() ) { $template_query->the_post(); $post_content = get_the_content(); if ( has_shortcode( $post_content, 'availability' ) ) { $template_id = get_the_ID(); //var_dump($template_id); } } wp_reset_postdata(); } } …
Category: Web

Running Filter or Shortcode Before Query Prevents Excerpt from Showing

I've been wrestling with some code cobbled together for a function I found, first published circa 2009 and my inexperience with PHP has me at a loss. The function is designed to count episodes in a podcast category, thus: function Get_Post_Number($postID){ $temp_query = $wp_query; $postNumberQuery = new WP_Query(array ( 'orderby' => 'date', 'order' => 'ASC', 'post_type' => 'any', 'category_name' =>'podcast-episodes', 'posts_per_page' => '-1' )); $counter = 1; $postCount = 0; if($postNumberQuery->have_posts()) : while ($postNumberQuery->have_posts()) : $postNumberQuery->the_post(); if ($postID == get_the_ID()){ …
Category: Web

How to Use PHP Code In-Page?

I am looking to create a dynamic Add-to-Cart button. The problem is that I need my server to dynamically create the "add-to-cart" URL. Currently, I have a static example built here at this link. You'll notice at the bottom of the page is a pay-per-post function, and an "Unlock Full Article" button. I would like this button to act as an add-to-cart button. The only problem I am having thus far, is how to dynamically create the correct URL extension? …
Category: Web

How to get the Clicked ID and output that ID data in another page

I am creating a WordPress theme. I created single.php, in which I have a button called "Watch Now". When "Watch Now" is clicked, it gets the ID of the current/clicked post dynamically using the function get_the_ID(). When it goes to another page (receive.php), I want to output that ID data, i.e. image of that ID, content of that ID, title and other things. However, when I click "Watch Now", it leads me to receive.php with this error: Error 404 - …
Category: Web

Fatal error: Call to a member function get_queried_object_id()

I am trying to display shortcode using the following pattern: class callShortCode { function __construct() { global $post; $this->dos($post); } private function dos($post){ global $post; $pid = get_the_ID(); $archive_id = get_queried_object_id(); if($archive_id == $pid) { add_action('wp_footer', array($this, 'show')); } } // shortcode call function show($post) { $pid = get_the_ID(); $archive_id = get_queried_object_id(); $the_query = new WP_Query( array('post_type' => 'custom_post_type') ); if ($the_query->have_posts()) { while ($the_query->have_posts()){ $the_query->the_post(); if($pid == $archive_id) { echo do_shortcode("[my_shortcode id='5']"); } } } } } new callShortCode(); …
Category: Web

Function get_the_ID() doesn't return 404 and search pages

I have a large switch statement where I get all the pages using the function get_the_ID() but it doesn't work for 404 and search pages. This is my code: global $page, $paged, $post; $selectpage = get_the_ID(); switch ($selectpage) { case (is_single() || is_page()): break; //...... case (is_category()): break; case (is_search()): break; default: break; } I can use an if statement instead of a switch, but a switch doesn't re-evaluates the conditions. Also, I can use an if statement out of …
Category: Web

get_the_ID() is working on my local development but not on staging server

Great to see a StackExchange for WP! Ok so I'm working on pulling images and dates from piklist custom post types and I got this working on my local, BUT when I move it to WPEngine I get a white page until I take the code out. The date code doesn't throw a blank page but won't pull the dates entered. Can anyone tell me what about this code may be causing this? Thanks in advance! CODE single.php <?php while …
Category: Web

how to echo/display the custom field value in specific custom field name?

I have tried this code: <?php query_posts(array( 'meta_key' => 'custom_cat', 'meta_value' => 'creative', 'post_type' => 'page' )); echo '<ul class="clearfix row">'; if ( have_posts() ) while ( have_posts() ) : the_post(); echo '<li class="col-md-4">'; the_title(); the_content(); echo '<i class="'.get_post_meta( get_the_ID(), "fa_icon", true ).'"></i>'; echo '</li>'; endwhile; echo '</ul>'; wp_reset_query(); ?> to display the custom field value as a class name, but the value is not displaying. Please help me find the problem or solution to display the custom field value …
Category: Web

Extracting the post_id via the wp_insert_post action (external db query)

i'm writing a little action plugin. everytime a new post is created, a new table should be created in an external db. The name of the table sholud reflect the post_id of the newly created post, but i can0t figure how to pass it. Here's my code so far (for php purists, forgive me for using old mysql_connect (but adding the flag TRUE to the new_link parameter, i don't have to bother with wpdb globals etc) function insert_new_table($post_id){ $usernamel = …
Category: Web

Conditional using get_the_ID() not working

I'm using this script to not show title on a specific page <?php if (get_the_ID() != 50){ ?> Now I need to add another page: <?php if ((get_the_ID() != 50) || (get_the_ID() != 317)){ ?> But it's not working.
Category: Web

How to get page's ID if I know the title only?

Is there an exact opposite funciton to this one: get_the_title(ID) I know there's: get_the_id() But it doesn't seem to accept any arguments. So, basically, I'm looking for something like: $title = 'Something'; get_the_id($title); I already know this solution: global $wpdb; $post_name = get_query_var('name'); $post_id = $wpdb->get_var("SELECT ID FROM $wpdb->posts WHERE post_name = $post_name"); But maybe there's something bulit-in that I'm missing?
Category: Web

Wordpress returns the same Post ID every time everywhere!

There is something weird happening in a recent theme I have developed. Inside the post page when I use $post->ID and get_the_ID() it always returns the same ID which belongs to one of my posts! I'm wondering what's happening there?!!! Edit: I found the reason behind this behavior. And it is that I have created a widget which inside it I use the WP_Query class (you can see the code at the end) and I guess it's overwriting the global …
Category: Web

wp_get_current_user always returns 0 continued

I have been busting my head trying to get the user ID with every piece of code possible but it always comes back "0". Not sure what I'm doing wrong. I started out using this: <?php $user_info = get_userdata(1); echo 'Username: ' . $user_info->user_login . "\n"; echo 'User level: ' . $user_info->user_level . "\n"; echo 'User ID: ' . $user_info->ID . "\n"; ?> Which kicks back a bunch of Function undefined errors which I take care of with "require_once" calling …
Category: Web

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.