• Resolved gnjang

    (@gnjang)


    hello.
    We are developing post-type-search.php through custom post type.
    Here, the class name of the feature image or other text?? They are searched together.
    How to exclude this?

Viewing 6 replies - 1 through 6 (of 6 total)
  • Moderator bcworkz

    (@bcworkz)

    What code do you use to search? If you instantiate a WP_Query object you should be able to specify criteria to get back whatever you want. If WP_Query is inadequate for some reason, you can always make the desired SQL query directly using the global $wpdb object.

    Thread Starter gnjang

    (@gnjang)

    $args = array(

    “post_type” => $post_type,

    ‘posts_per_page’ => -1,

    ‘orderby’ => ‘date’,

    ‘order’ => ‘DESC’,

    ‘s’ => $s,

    );

    $list_query = new WP_Query($args);

    I am using it like this. What solutions are there?

    Moderator bcworkz

    (@bcworkz)

    What value is assigned to $post_type? I’m guessing it’s an array of types. If you want only your custom post type to be searched, set the “post_type” query var to be that of your post type and not $post_type. Then the search term $s will be looked for in your post type’s title, content, or excerpt fields. No attached images or other post types will be searched.

    Thread Starter gnjang

    (@gnjang)

    $post_type is a custom post type.
    However, unlike what you said, if you search by class name in the feature image, it is possible to search.

    example : img width=”296″ height=”168″ src=”https://dva.remotecall.com/kr/wp-content/uploads/sites/2/2023/09/image2.jpeg” class=”attachment-296×210 size-296×210 wp-post-image” alt=”” decoding=”async” loading=”lazy”>

    search text : att or attachment

    • This reply was modified 1 year, 2 months ago by gnjang.
    Moderator bcworkz

    (@bcworkz)

    Embedded image HTML is part of post content, it’s non-trivial to exclude from searches. You would need to search using a SQL REGEX function (regular expression) of some sort. WP_Query does not accept REGEX args when searching content, you’d need to develop your own custom search query.

    Alternately, you could go through the search results from WP_Query with PHP and remove any posts matching a regex. But this can mess up pagination, it’s better done through SQL.

    Either way, excluding img tags from search will significantly slow down search requests. In theory it’s possible, but it’s complicated, I wouldn’t recommend it.

    Thread Starter gnjang

    (@gnjang)

    Thank you for answer..

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘How to exclude image search’ is closed to new replies.