Post a WP request through a custom post type that works on the home page but not on the search page

I have two custom message types: Artists and Painting . Both types of messages have a custom field called the artist name created using the Advanced Custom Fields plugin. I need to be able to map custom fields from both of these message types to each other in order to display additional information.

The box below only the arguments and loops from the query. If you want, add more code.

<?php
$artist_name = get_field('artist');

$args = array(
'post_type' => 'artists',
'meta_value' => $artist_name
);
$query_artist = new WP_Query( $args );

if ( $query_artist->have_posts() ) {
    while ( $query_artist->have_posts() ) {
        $query_artist->the_post(); ?>
        <p class="artist-name"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></p>
        <?php }
} else {
    echo 'Artist not found';
}
wp_reset_postdata(); ?>

, , " " . , . . -, , , ?

.

+4
2

, , , , , , .

, - :

<?php
// Permalink for artist
$artist_name = get_field('artist');
global $post;
$posts = get_posts( array( 'post_type' => 'artists', 'meta_value' => $artist_name ) );
if( $posts ):
   foreach( $posts as $post ) :   
    setup_postdata($post); ?>
    <p class="artist-name"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></p>
   <?php endforeach; 
wp_reset_postdata(); 
endif; ?>
+4

, wordpress .

https://wordpress.org/plugins/advanced-custom-post-search/ functions.php

function rc_add_cpts_to_search($query) {     
    // Check to verify it search page
    if( is_search() ) {
        // Get post types
        $post_types = get_post_types(array('public' => true, 'exclude_from_search' => false), 'objects');
        $searchable_types = array();
        // Add available post types
        if( $post_types ) {
            foreach( $post_types as $type) {
                $searchable_types[] = $type->name;
            }
        }
        $query->set( 'post_type', $searchable_types );
    }
    return $query;
}
add_action( 'pre_get_posts', 'rc_add_cpts_to_search' );

http://www.remicorson.com/include-all-your-wordpress-custom-post-types-in-search/

+1

Source: https://habr.com/ru/post/1533491/


All Articles