1

We often use the Oembed function provided by the Wordpress to embed media.

I have a condition where I am running a loop to fetch certain posts from a custom post type, but I want to skip posts in which Oembed is empty.

How to check if Oembed is empty or has a URL in meta of single.php.

I tried something like this →

<?php $url = esc_url( get_post_meta( get_the_ID(), 'video_oembed', true ) ); ?>
<?php $embed = wp_oembed_get( $url ); ?>

PHP Needed →

Now how to check if $embed is empty or not?

Update as requested →

The front end will be like this, and it pulled by running a WP custom loop like this.

But there can be a situation like this where the OEMBED is empty like this.

So what I want is when such situation exists the loop should exclude that post and for that, we need if condition that checks If oembed is empty or not.

enter image description here

2

The wp_oembed_get() only works for supported oEmbed providers. The return value is also is a URL of false, as mentioned per codex:

If $url is a valid url to a supported provider, the function returns the embed code provided to it from the oEmbed protocol. Otherwise, it will return false.

Therefore, is the input is empty, the return value would be false, so you could simply check:

if ( $embed ) {
    // Valid
}
1

It’s a little hard to guess what’s your question really is and what exactly you want to check...

If you want to check if $embed is empty, just check it:

if ( ! trim($embed) ) {
    // it’s empty
}

If you want to check if the $url is empty:

if ( ! trim($url) ) {
    // it’s empty
}
  • I have a video CPT, where video URL needs to be embedded, but if the post is empty w/o video URL in Oembed than that posts need to e exist in the loop while fetching all the posts so I was looking for a condition so that empty posts w/o Oembed should be left without. – The WP Novice Jun 3 '18 at 6:29
  • I tried your solution, but it didn't worked. – The WP Novice Jun 3 '18 at 6:34
  • Can you help me to improve the question since you downvoted me. – The WP Novice Jun 3 '18 at 6:35
  • @TheWPNovice I’m sure it worked. But I’m almost certain it didn’t do what you wanted - I still have no idea what are you trying to achieve... it’s really hard to guess anything based on your question and the comment above... – Krzysiek Dróżdż Jun 3 '18 at 6:36
  • Ok let me try to elaborate. Give me 5 minutes please. Thanks – The WP Novice Jun 3 '18 at 6:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.