Get media from two posts with same image












0















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?










share|improve this question



























    0















    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?










    share|improve this question

























      0












      0








      0








      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?










      share|improve this question














      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






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 21 '18 at 15:59









      JesperJesper

      659727




      659727
























          1 Answer
          1






          active

          oldest

          votes


















          0














          get_attached_media() accepts a post_id as an argument, ie:



          get_attached_media('image',$post_id);





          share|improve this answer
























          • 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 - 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













          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
          });


          }
          });














          draft saved

          draft discarded


















          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









          0














          get_attached_media() accepts a post_id as an argument, ie:



          get_attached_media('image',$post_id);





          share|improve this answer
























          • 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 - 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


















          0














          get_attached_media() accepts a post_id as an argument, ie:



          get_attached_media('image',$post_id);





          share|improve this answer
























          • 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 - 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
















          0












          0








          0







          get_attached_media() accepts a post_id as an argument, ie:



          get_attached_media('image',$post_id);





          share|improve this answer













          get_attached_media() accepts a post_id as an argument, ie:



          get_attached_media('image',$post_id);






          share|improve this answer












          share|improve this answer



          share|improve this answer










          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 - 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





















          • 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 - 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



















          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






















          draft saved

          draft discarded




















































          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.




          draft saved


          draft discarded














          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





















































          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







          Popular posts from this blog

          How to change which sound is reproduced for terminal bell?

          Can I use Tabulator js library in my java Spring + Thymeleaf project?

          Title Spacing in Bjornstrup Chapter, Removing Chapter Number From Contents