Always Encounter Using ID in Wordpress as a Final Solution

admin2025-06-02  1

Closed. This question needs details or clarity. It is not currently accepting answers.

Want to improve this question? Add details and clarify the problem by editing this post.

Closed 6 years ago.

Improve this question

I am learning Wordpress Development.

I am noticing whenever I encountered a problem I usually check the web for similar issue(s) for the solution.

Anyway, what I want to ask is, I've noticed, most of the time a solution of using ID is being used. I am thinking that isn't a general solution if that theme will be used by other people and on their Wordpress machine/environment, those IDs will change.

So my question is, why are people accepting ID as a final solution. I think that isn't the best dynamic solution since it will change. Whether it's Page ID, Post ID, every pre-made function that uses ID.

And, is there any better dynamic solution?

Or I am missing something here that ID won't change?

Thanks for the responses in advance. I would love to hear your thought(s) on this as it's been probably 2months this thing is bugging my mind.

Closed. This question needs details or clarity. It is not currently accepting answers.

Want to improve this question? Add details and clarify the problem by editing this post.

Closed 6 years ago.

Improve this question

I am learning Wordpress Development.

I am noticing whenever I encountered a problem I usually check the web for similar issue(s) for the solution.

Anyway, what I want to ask is, I've noticed, most of the time a solution of using ID is being used. I am thinking that isn't a general solution if that theme will be used by other people and on their Wordpress machine/environment, those IDs will change.

So my question is, why are people accepting ID as a final solution. I think that isn't the best dynamic solution since it will change. Whether it's Page ID, Post ID, every pre-made function that uses ID.

And, is there any better dynamic solution?

Or I am missing something here that ID won't change?

Thanks for the responses in advance. I would love to hear your thought(s) on this as it's been probably 2months this thing is bugging my mind.

Share Improve this question edited Feb 28, 2019 at 4:10 greenman0914 asked Feb 27, 2019 at 11:36 greenman0914greenman0914 112 bronze badges 2
  • It depends upon the requirements. Sometime a single user want a small change and other won't. So that time we use hard code id. – Jitender Singh Commented Feb 27, 2019 at 11:48
  • Thank you for taking the time to answer. This was very helpful and had cleared this aspect for now, now I can leave it! :) I think I cannot comment to give thanks to each and every one who had answered my question as per this website's rule. This comment goes to you all! – greenman0914 Commented Feb 28, 2019 at 4:02
Add a comment  | 

2 Answers 2

Reset to default 1

You're absolutely correct that hard-coding specific IDs is a bad idea. IDs are very much subject to change, and normal activity through the UI can cause problems with any code expecting a specific ID, which makes them fragile.

However, when you see solutions involving a specific ID, you need to keep in mind that the scope of many of these solutions, especially on this site, is limited to only the specific problem that the user is asking about.

As a very simple example, a user might ask "How do I print the title of a specific page?", and the solution proposed will be something like:

echo get_the_title( 12 );

Where 12 is the ID of a specific page.

Hard-coding the ID 12 is indeed a bad idea, however all this solution is intended to tell the user is that you can output a page title — given its ID — using the function get_the_title(). Where that ID comes from is a separate issue to the question that was asked.

So, when implementing that solution, the user is probably going to need to come up with a way to allow that ID to be dynamic. Exactly how they do that is a separate issue to the problem the solution was regarding, and will depend on context. If they needed help with that on this site, then that would require a separate question (if it hasn't already been asked, which it almost certainly has).

The user might want to get the ID from the current post, or maybe it will be a value saved in the database through the Customiser or Settings API, or possibly passed through the URL in the query string. There's lots of options, but the point is that how to get dynamic IDs is a separate problem to how to do something with an ID, and most solutions expect that if you need an ID to be dynamic, that you'd just combine the solution with any of those solutions for making an ID dynamic.

That's another thing to keep in mind: The point of sites like this is to help your learn how to develop with WordPress. It's not to just do free work and give you code to put into your site or project. Any given answer is likely focused on specific questions being asked, and won't be a fully-formed drop-in solution. The question asker is going to need to use some development skills to properly integrate the solution into their project.

Anyway, what I want to ask is, I've noticed, most of the time a solution of using ID is being used. I am thinking that isn't a general solution if that theme will be used by other people and on their Wordpress machine/environment, those IDs will change.

ID's of various objects remains unchanged within a wordpress install. So it does not matter were a theme is used.

Or I am missing something here that ID won't change?

ID does not change because it is unique identifier for an object (post, page, taxonomy, term, user, menu, site etc) in database.

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1748855987a314286.html

最新回复(0)