WordPress.org

Ready to get started?Download WordPress

Forums

not display post repeateated post by custom field value (4 posts)

  1. andrescifuentesr
    Member
    Posted 1 year ago #

    Hi everyone

    It's been a coupel of thays and i stuck with something. Im using a custom field to show some videos in some articles. So for, it's good.
    In the other hand, i have a special template where i show all the videos that I display in all the posts.
    The problem appear when i had to show a same video in different post because oubviously is going to be display several times in my video template.
    Is the any way of avoiding this?
    I've tryed using 'meta_compare' => '!=' but not working.

    Any advise would be appreciated.

    thanks

  2. keesiemeijer
    moderator
    Posted 1 year ago #

    Can you show the query you're using. Try it with a meta_query:
    http://codex.wordpress.org/Function_Reference/WP_Query#Custom_Field_Parameters
    Maybe the compare 'NOT EXISTS' parameter does what you need.

  3. andrescifuentesr
    Member
    Posted 1 year ago #

    thanks for your answer.
    So far is doing something but not what I expect.
    I have 3 videos, two of them are repeated as I say in the initial post. When I change the compare parameter to NOT EXISTS, now it only shows the value that is not repeated, but no value for the repeated one.

    here is my query :

    $args = array(
    'posts_per_page' => -1,
    'post_type' => 'post',
    'meta_key' => 'video',
    'meta_value' => 'NULL',
    'meta_compare' => 'NOT EXISTS',
    );

    $queryVideos = new WP_Query( $args );

    while ( $queryVideos->have_posts() ) : $queryVideos->the_post();
    ....

  4. andrescifuentesr
    Member
    Posted 1 year ago #

    finllay i used this:

    <?php
    						$videos = get_posts(array(
    						'post_type' 	=> 'post',
    						'orderby'       => 'Id',
    						'order'			=> 'DESC',
    						'posts_per_page'  	=> -1,
    						'meta_query' => array(
    							array(
    								'key' => 'video', // name of custom field
    								'value' => ' ',
    								'compare' => '!='
    							)
    						)
    						));
    
    						$videos_names = array();
    
    						if( $videos )
    						{
    							foreach( $videos as $video )
    							{
    								// get the video
    								$videos_name = get_field('video' , $video->ID);
    
    								// add video to $videos, only if it doesn't already exist
    								if( !in_array($videos_name, $videos_names) )
    								{
    									$videos_names[] = $videos_name;
    								}
    
    							}
    						}
    
    					?>
    
    					<?php if( $videos_names ): ?>
    						<ul>
    						<?php foreach( $videos_names as $videos_name ): ?>
    
    							<li class="video-carrousel">
    								<?php echo $videos_name ?>
    							</li>
    
    						<?php endforeach; ?>
    						</ul>
    					<?php endif; ?>

Topic Closed

This topic has been closed to new replies.

About this Topic