PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Friday, July 8, 2022

[FIXED] How to make wordpress function working only on single & NOT for pages

 July 08, 2022     function, php, posts, wordpress     No comments   

Issue

I'm struggling with one thing. I've got such wordpress function:

   function wpq_insert_attachment_data($data, $postarr){
if (!is_single() ) {
    $posttitle = get_the_title( $postarr['post_parent'] );
    $data['post_title'] = $posttitle;
    $data['post_name'] = $posttitle;    
    return $data;        
}}  

add_filter( 'wp_insert_attachment_data', 'wpq_insert_attachment_data', 10, 2 );

It works superb but it covers all single/custom post type/pages etc. Is there any way to EXCLUDE pages from that function? I've tried sorting it out with is_single() yet without success.


Solution

Use is_singular in your statement to target specific post types.

Can also do an array to include or exclude.

If  (! is_singular()) ..... or.....
 (! is_singular(array('page','food',foo')))

Then it will only run on the singles for whichever post type you're targeting.



Answered By - Crossifixio
Answer Checked By - Cary Denson (PHPFixing Admin)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing