Support » Plugin: YARPP - Yet Another Related Posts Plugin » wp_query not working for some reason

  • Resolved qrmiz


    Hello! I really love this plugin. Easy to use, easy to amend, although I faced an issue and can’t find a way to fix it…

    inside yarpp template I have this code:

    $mainaudio = new WP_Query(array('posts_per_page' => '1','post_parent' => $post->ID,'post_status' => 'inherit','post_type' => 'attachment', 'post_mime_type' => 'audio', 'orderby' => 'menu_order', 'order' => 'ASC', 'meta_query' => array(array('key' => 'voice-reel','value' => 'Main Reel','compare' => 'LIKE',))));
    if ( $mainaudio->have_posts() ) :
    	echo "<span>Voice Sample</span>";
    	while ($mainaudio->have_posts()) : $mainaudio->the_post();
    	$audiourl = addslashes(wp_get_attachment_url( $post->ID ));
    	echo do_shortcode( '[audio src="' . $audiourl . '"]' );
    	else: echo "no posts";	

    It is supposed to pull audio attachment and display it as audio shortcode. Code is fine, since it displays and works on other pages, but as soon as I place it in yarpp template, it doesn’t give me the results. I feel like it doesn’t like ‘new WP_Query’ callout… is there a way to fix it?

    Thank you a lot!

Viewing 4 replies - 1 through 4 (of 4 total)
  • Plugin Author Michael Nelson


    Hi @qrmiz, sorry for the trouble, but thanks for explaining the issue.
    I looked in our code for a bit, and used a modified version of your code sample, and have come up with a solution. At the start of your code, just add

    global $yarpp;

    While YARPP is fetching related posts, it uses some filters to modify WP_Query to instead show related posts (instead of just most recent posts). However, it currently only removes those filters AFTER it’s done showing related posts… so when you were using WP_Query those filters were again only showing related voice samples (in addition to all the criteria you added to WP_Query). So the code snippet I provided tells YARPP to stop doing that.
    It’s ok to do that after YARPP has already found the related posts.

    I’m also investigating if we can change YARPP’s code to prevent you needing to add the band-aid fix I suggested…

    Thread Starter qrmiz


    Hello Michael,

    this worked! Thank you very much for quick reply and support!
    Amazing plugin and great author, really appreciate what you’re doing!


    Plugin Author Michael Nelson


    Great to hear it worked for you. (FYI YARPP has quite a few people working on it, and has had lots of previous people working on it for a while before that…)
    Anyways, our next update of YARPP should have a fix in it which will mean you won’t need to use the code snippet I suggested in my last comment. Hopefully, that will also prevent future users from having similar problems.
    Thanks, take care!

    Plugin Author yarpp


    Hi @qrmiz the latest version of YARPP includes this fix. So you don’t need the custom code snippet any more 🙂

Viewing 4 replies - 1 through 4 (of 4 total)
  • You must be logged in to reply to this topic.