Get media from two posts with same image
Let's say I create a new post and upload an image to it. Now I can get the image's source URL by calling get_attached_media
with the post's ID or by calling the API at /wp-json/wp/v2/media?parent=$ID
.
If I create a new post and use the same image, how can I get the source URL for the image from the ID of the second post?
wordpress wordpress-rest-api
add a comment |
Let's say I create a new post and upload an image to it. Now I can get the image's source URL by calling get_attached_media
with the post's ID or by calling the API at /wp-json/wp/v2/media?parent=$ID
.
If I create a new post and use the same image, how can I get the source URL for the image from the ID of the second post?
wordpress wordpress-rest-api
add a comment |
Let's say I create a new post and upload an image to it. Now I can get the image's source URL by calling get_attached_media
with the post's ID or by calling the API at /wp-json/wp/v2/media?parent=$ID
.
If I create a new post and use the same image, how can I get the source URL for the image from the ID of the second post?
wordpress wordpress-rest-api
Let's say I create a new post and upload an image to it. Now I can get the image's source URL by calling get_attached_media
with the post's ID or by calling the API at /wp-json/wp/v2/media?parent=$ID
.
If I create a new post and use the same image, how can I get the source URL for the image from the ID of the second post?
wordpress wordpress-rest-api
wordpress wordpress-rest-api
asked Nov 21 '18 at 15:59
JesperJesper
659727
659727
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
get_attached_media() accepts a post_id as an argument, ie:
get_attached_media('image',$post_id);
Thought it was clear from my question, but I know that. The problem is, in my example, if I use the post ID of post 2, it won't return the image's source URL because it is attached to post 1.
– Jesper
Nov 21 '18 at 16:42
So why not just pass the post_id of post1 then?
– Richard Zack
Nov 21 '18 at 16:45
The question is a bit confusing. What code have you tried? Perhaps by posting your current attempts, we can better understand what you're trying to do.
– Richard Zack
Nov 21 '18 at 16:46
Let's say I send a request to my API to get images from post 2 and there are 100 posts. It doesn't return any image URLs, because the image I used in post 2 is attached to some other post. In this situation I have no way of knowing which post the image I am trying to get the URL of belongs to, so I can't just call the API at/wp-json/wp/v2/media?parent=$ID
- or in your case - callget_attached_media('image',$post_id);
, because I don't know the correct ID. I hope it makes sense.
– Jesper
Nov 21 '18 at 16:51
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53415961%2fget-media-from-two-posts-with-same-image%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
get_attached_media() accepts a post_id as an argument, ie:
get_attached_media('image',$post_id);
Thought it was clear from my question, but I know that. The problem is, in my example, if I use the post ID of post 2, it won't return the image's source URL because it is attached to post 1.
– Jesper
Nov 21 '18 at 16:42
So why not just pass the post_id of post1 then?
– Richard Zack
Nov 21 '18 at 16:45
The question is a bit confusing. What code have you tried? Perhaps by posting your current attempts, we can better understand what you're trying to do.
– Richard Zack
Nov 21 '18 at 16:46
Let's say I send a request to my API to get images from post 2 and there are 100 posts. It doesn't return any image URLs, because the image I used in post 2 is attached to some other post. In this situation I have no way of knowing which post the image I am trying to get the URL of belongs to, so I can't just call the API at/wp-json/wp/v2/media?parent=$ID
- or in your case - callget_attached_media('image',$post_id);
, because I don't know the correct ID. I hope it makes sense.
– Jesper
Nov 21 '18 at 16:51
add a comment |
get_attached_media() accepts a post_id as an argument, ie:
get_attached_media('image',$post_id);
Thought it was clear from my question, but I know that. The problem is, in my example, if I use the post ID of post 2, it won't return the image's source URL because it is attached to post 1.
– Jesper
Nov 21 '18 at 16:42
So why not just pass the post_id of post1 then?
– Richard Zack
Nov 21 '18 at 16:45
The question is a bit confusing. What code have you tried? Perhaps by posting your current attempts, we can better understand what you're trying to do.
– Richard Zack
Nov 21 '18 at 16:46
Let's say I send a request to my API to get images from post 2 and there are 100 posts. It doesn't return any image URLs, because the image I used in post 2 is attached to some other post. In this situation I have no way of knowing which post the image I am trying to get the URL of belongs to, so I can't just call the API at/wp-json/wp/v2/media?parent=$ID
- or in your case - callget_attached_media('image',$post_id);
, because I don't know the correct ID. I hope it makes sense.
– Jesper
Nov 21 '18 at 16:51
add a comment |
get_attached_media() accepts a post_id as an argument, ie:
get_attached_media('image',$post_id);
get_attached_media() accepts a post_id as an argument, ie:
get_attached_media('image',$post_id);
answered Nov 21 '18 at 16:39
Richard ZackRichard Zack
1669
1669
Thought it was clear from my question, but I know that. The problem is, in my example, if I use the post ID of post 2, it won't return the image's source URL because it is attached to post 1.
– Jesper
Nov 21 '18 at 16:42
So why not just pass the post_id of post1 then?
– Richard Zack
Nov 21 '18 at 16:45
The question is a bit confusing. What code have you tried? Perhaps by posting your current attempts, we can better understand what you're trying to do.
– Richard Zack
Nov 21 '18 at 16:46
Let's say I send a request to my API to get images from post 2 and there are 100 posts. It doesn't return any image URLs, because the image I used in post 2 is attached to some other post. In this situation I have no way of knowing which post the image I am trying to get the URL of belongs to, so I can't just call the API at/wp-json/wp/v2/media?parent=$ID
- or in your case - callget_attached_media('image',$post_id);
, because I don't know the correct ID. I hope it makes sense.
– Jesper
Nov 21 '18 at 16:51
add a comment |
Thought it was clear from my question, but I know that. The problem is, in my example, if I use the post ID of post 2, it won't return the image's source URL because it is attached to post 1.
– Jesper
Nov 21 '18 at 16:42
So why not just pass the post_id of post1 then?
– Richard Zack
Nov 21 '18 at 16:45
The question is a bit confusing. What code have you tried? Perhaps by posting your current attempts, we can better understand what you're trying to do.
– Richard Zack
Nov 21 '18 at 16:46
Let's say I send a request to my API to get images from post 2 and there are 100 posts. It doesn't return any image URLs, because the image I used in post 2 is attached to some other post. In this situation I have no way of knowing which post the image I am trying to get the URL of belongs to, so I can't just call the API at/wp-json/wp/v2/media?parent=$ID
- or in your case - callget_attached_media('image',$post_id);
, because I don't know the correct ID. I hope it makes sense.
– Jesper
Nov 21 '18 at 16:51
Thought it was clear from my question, but I know that. The problem is, in my example, if I use the post ID of post 2, it won't return the image's source URL because it is attached to post 1.
– Jesper
Nov 21 '18 at 16:42
Thought it was clear from my question, but I know that. The problem is, in my example, if I use the post ID of post 2, it won't return the image's source URL because it is attached to post 1.
– Jesper
Nov 21 '18 at 16:42
So why not just pass the post_id of post1 then?
– Richard Zack
Nov 21 '18 at 16:45
So why not just pass the post_id of post1 then?
– Richard Zack
Nov 21 '18 at 16:45
The question is a bit confusing. What code have you tried? Perhaps by posting your current attempts, we can better understand what you're trying to do.
– Richard Zack
Nov 21 '18 at 16:46
The question is a bit confusing. What code have you tried? Perhaps by posting your current attempts, we can better understand what you're trying to do.
– Richard Zack
Nov 21 '18 at 16:46
Let's say I send a request to my API to get images from post 2 and there are 100 posts. It doesn't return any image URLs, because the image I used in post 2 is attached to some other post. In this situation I have no way of knowing which post the image I am trying to get the URL of belongs to, so I can't just call the API at
/wp-json/wp/v2/media?parent=$ID
- or in your case - call get_attached_media('image',$post_id);
, because I don't know the correct ID. I hope it makes sense.– Jesper
Nov 21 '18 at 16:51
Let's say I send a request to my API to get images from post 2 and there are 100 posts. It doesn't return any image URLs, because the image I used in post 2 is attached to some other post. In this situation I have no way of knowing which post the image I am trying to get the URL of belongs to, so I can't just call the API at
/wp-json/wp/v2/media?parent=$ID
- or in your case - call get_attached_media('image',$post_id);
, because I don't know the correct ID. I hope it makes sense.– Jesper
Nov 21 '18 at 16:51
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53415961%2fget-media-from-two-posts-with-same-image%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown