Home Forums ACF PRO add related content to get_posts query


add related content to get_posts query

  • Hello,

    I’m using a category slider which queries posts by a specified categoriy in a loop:

    		//Get Posts
    		$posts = get_posts( array(
    			'posts_per_page'   => $args['limit'],
    			'offset'           => 0,
    			'category'         => $category_term->term_id,
    			'orderby'          => $args['order_by'],
    			'order'            => $args['order'],
    			'include'          => '',
    			'exclude'          => '',
    			'meta_key'         => '',
    			'meta_value'       => '',
    			'post_type'        => 'post',
    			'post_mime_type'   => '',
    			'post_parent'      => '',
    			'post_status'      => 'publish',
    			'suppress_filters' => true

    the_field(‘rel_content’) return the associated post IDs.

    What’s the smart way to include related posts to my slider?

    I tried the get_posts ‘include’ array but it didn’t work or maybe I’m just doing something wrong. Is it possible to enqueue the related content just by the ACF rel_content IDs?

    best regards

  • There isn’t any way to do this directly in a single query. You need to first do the query that you’re doing. Then loop through the results of that query to get the related posts from the acf fields and then do either merge those results into the results of the first query or collect all the post IDs of the first query and from getting the related posts and do another query using that list.

  • thanks John, do you mean creating a second array by the same structure and then merging both arrays? But then I should better use “Post Objects” instead of “Post IDs”, right?

    edit: I got it by setting “Post Objects” and merging both arrays

    $related = ( get_field(‘rel_content’) );
    $result = array_merge($related, $posts);

    Now I need only a way to order the result and render the
    rel_content array first, before posts.

    //Get Attributes
    	$args = shortcode_atts(
    		array(//Set default
    			'category' => 'common',
    			'order_by' => 'post_date',
    			'order' => 'DESC',
    			'limit' => 10,
    			'text_length' => 200,
    			'link' => 'true',
    			'slide_duration' => 2000,
    			'auto_start' => 'true',
    			'start_time' => 60000,
    			'cycle_time' => 10000,
    			'show_headers' => 'true',
    			'image_size'	   => 'large',
    			'thumbnail_size'   => 'thumbnail',
    			'thumbnail_menu'   => 'true'

    Can I use the $args section to search for the rel_content fields and priorize that posts?

  • I don’t know what the code you posted is.

    What I mean is something along the lines of

    // get your posts
    $posts = get_posts(...);
    // loop through the posts and build list of related posts
    $related = array();
    foreach ($posts as $post) {
      $related = array_merge($related, get_field('related_posts', $post->ID));
    $posts = array_merge($posts, $related);

    There are a number or ways you can do this depending on what you need. But there isn’t any configuration of arguments that will get all of the posts and all of the posts related to those posts in a single query.

Viewing 4 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic.