Support

Account

Home Forums Add-ons Repeater Field Author page with custom post type subfield filter meta query in m

Solved

Author page with custom post type subfield filter meta query in m

  • Hi.

    I have problem on author.php page. I have custom post type and there is repeater filed “users” with subfield “user_name” (type “User”).
    I want to add filter in meta_query on author.php page if that subfield exist and if it is equal to current author page to show all those posts.

    Why I need that – For each author I want to have all post which that author was created and also all posts where that user is selected in subfield “user_name”.

    author.php code:

    <div class="post_article">
            
            <?php
                $authorid = get_the_author_meta('ID');
    			$cat = get_queried_object();
                $args =  array(
                    "post_type" => "any",
                    "post_status" => "publish",
                    "orderby" => "date",
                    "order" => 'DESC',
                    "posts_per_page" => 12,
                    "author" =>  $authorid,
    		'cat' => $cat->term_id
                );
    
                $query = new WP_Query($args);
    		?>
    		
                    <?php while ($query->have_posts()) : $query->the_post(); 
    						echo get_the_author();?>
    		
                        <article class="post">
    						<div class="category_thumb">
    						<a class="post_thumb_list" href="<?php the_permalink() ?>">
    							<?php the_post_thumbnail('medium');?>
    						</a>
    						</div>
    						<div class="entry_content">
    							<header class="entry_header">
    								<?php 
    									$post_title = get_the_title();
    								?>
    								<h2><a href="<?php the_permalink() ?>" ><?php echo do_shortcode($post_title); ?></a></h2>
    							</header>
    
    						</div>
    					</article>
    
                    <?php endwhile; ?>
    
    		   </div>
  • Thanks.
    I solved the problem.

    I added this in function.php:

    function comapa_posts_where( $where ) {
    	$where = str_replace("meta_key = 'users_$", "meta_key LIKE 'users_%", $where);
    	return $where;
    }
    
    add_filter('posts_where', 'comapa_posts_where');

    And I modified query on author.php:

    
            <?php
    	        global $wpdb;
        		$author_name = get_the_author();
                $authorid = get_the_author_meta('ID');
    			$cat = get_queried_object();
                $args =  array(
                    'post_type' => "any",
                    'post_status' => "publish",
                    'orderby' => "date",
                    'order' => 'DESC',
                    'posts_per_page' => 12,
    				'cat' => $cat->term_id,
    				'meta_query' => array(
    				
    		          array(
    		               'key' => 'users_$_user_name',
    		               'value' => $authorid,
    		               'compare' => '='
    		          )
    		          
    				 
    				)
    				
                );
    
                $query = new WP_Query($args);
             
    		?>
    
Viewing 3 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic.