• Resolved kc11

    (@kc11)


    Hi,

    I am writing a plugin which searches for a keyword in a post. When it runs the first time the plugin will find and extract the keyword, if its present. After extracting the keyword, there is no further need to run the extraction function. I would like to set some sort of global flag so that the next time the plugin runs, it know not to search for or extract the keyword. Ideally the entire plugin could even be deactivated at this point.

    Does anyone have any suggestions on best practices ( or practices ) ??

    Best regards,

    KC

Viewing 6 replies - 1 through 6 (of 6 total)
  • When does your plugin scan the posts, when you query them for display or via hooks when they are published and/or saved?

    If you’re using hooks to run at post insertion and update i’d suggest using post meta(if not, why not?). Prefix the meta key with an underscore and it’ll automatically get excluded from general display.

    Your flag would be a post meta key -> value pair that represents whether the script has run, and assumption could be made that if the key doesn’t exist it’s the first time scanning the given post for your keyword..

    Meta functions:
    https://codex.www.ads-software.com/Custom_Fields#Function_Reference

    NOTE: The get_post_custom functions do not exclude unscored keys(you won’t need any of those for a single key though).

    Thread Starter kc11

    (@kc11)

    Hi Mark,

    following your directions, I was able to create a custom field for my keyword named (surprisingly) ‘keyword’. I am able to store a keyword and retrieve it later. Thank you.

    I am now trying to write a sql query using inner join as an experiment. I’d like to select all post ID numbers, in the posts table which have a custom field named ‘keyword’

    new1 =  $wpdb->get_col("SELECT ID FROM $wpdb->posts INNER JOIN $wpdb->postmeta where meta_key='keyword'");

    when I do a var_dump, the output is as follows:

    NEW1

    array
    0 => string ‘1’ (length=1)
    1 => string ‘2’ (length=1)
    2 => string ‘3’ (length=1)
    3 => string ‘4’ (length=1)
    4 => string ‘5’ (length=1)
    5 => string ‘6’ (length=1)
    6 => string ‘7’ (length=1)
    7 => string ‘8’ (length=1)

    I was expecting only 1 record to be returned. Any idea what I am doing wrong?

    Thank you.

    KC

    If you only want one result then why select a column of data? (it would be one result if only one post matched, obviously you have more than one match).

    https://codex.www.ads-software.com/Function_Reference/wpdb_Class

    You should ideally use get_posts or WP_Query for querying posts, unless there’s some specificity you need inside the query that goes beyond the capabilities of those functions.

    https://codex.www.ads-software.com/Function_Reference/get_posts
    https://codex.www.ads-software.com/Function_Reference/WP_Query

    Thread Starter kc11

    (@kc11)

    Hi Mark,

    As always, you have a valid point. I am doing this as an aside to my plugin , just to try doing a table join. I’ve never done one before, and I’m not sure if I’m using the syntax correctly or if my logic makes sense. By the way does ID in the wp_posts table correspond with post_id in the wp_postmeta table? I’m assuming it does.

    KC

    jocken

    (@jocken)

    The query_posts and get_posts function are ideal for this kind of tasks.

    Thread Starter kc11

    (@kc11)

    Hi Guys,

    I think I figured out the syntax. The following appears to work:

    $new1 =  $wpdb->get_col("SELECT ID FROM $wpdb->posts INNER JOIN $wpdb->postmeta ON  ID=post_id WHERE meta_key='keyword'");

    As you have suggested, I am going to try to move toward using the built in functions and WPquery object. One question though: From reading the codex links you have attached , Mark, the focus of the Wpquery object appears to be on the WP_posts table. Can WP_query also be used to query other tables?

    Thank you,

    KC

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘How to set a global condition flag’ is closed to new replies.